Note that there are some explanatory texts on larger screens.

plurals
  1. POsed recipe: how to do stuff between two patterns that can be either on one line or on two lines?
    primarykey
    data
    text
    <p>Let's say we want to do some substitutions only between some patterns, let them be <code>&lt;a&gt;</code> and <code>&lt;/a&gt;</code> for clarity... (<em>all right, all right, they're <code>start</code> and <code>end</code>!.. Jeez!</em>)</p> <p>So I know what to do if <code>start</code> and <code>end</code> always occur on the same line: just design a proper regex.</p> <p>I also know what to do if they're guaranteed to be on different lines and I don't care about anything in the line containing <code>end</code> and I'm also OK with applying all the commands in the line containing <code>start</code> <strong>before</strong> <code>start</code>: just specify the address range as <code>/start/,/end/</code>.</p> <p>This, however, doesn't sound very useful. What if I need to do a smarter job, for instance, introduce changes inside a <code>{...}</code> block?</p> <p>One thing I can think of is breaking the input on <code>{</code> and <code>}</code> before processing and putting it back together afterwards:</p> <pre><code>sed 's/{\|}/\n/g' input | sed 'main stuff' | sed ':a $!{N;ba}; s/\n\(}\|{\)\n/\1/g' </code></pre> <p>Another option is the opposite:</p> <pre><code>cat input | tr '\n' '#' | sed 'whatever; s/#/\n/g' </code></pre> <p>Both of these are ugly, mainly because the operations are not confined within a single command. The second one is even worse because one has to use some character or substring as a "newline holder" assuming it isn't present in the original text.</p> <p>So the question is: are there better ways or can the above-mentioned ones be optimized? This is quite a regular task from what I read in recent SO questions, so I'd like to choose the best practice once and for all.</p> <p>P.S. I'm mostly interested in pure <code>sed</code> solutions: can the job be do with one invocation of <code>sed</code> and nothing else? Please no <code>awk</code>, <code>Perl</code>, etc.: this is more of a theoretical question, not a "need the job done asap" one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload