Let's say we want to make some substitutions only between some patterns, let them be <a> and </a> for clarity ... (everything is fine, everything is fine, they are start and end !. Jeez!)
So, I know what to do if start and end always occur on the same line: just create the correct regular expression.
I also know what to do if they are guaranteed on different lines, and I donβt care about the line containing end , and I am fine with all the commands in the line containing start to start : just specify the address range as /start/,/end/ .
This, however, is not very useful. What should I do if I need to do smarter work, for example, make changes inside the {...} block?
One thing I can think of is to break the input into { and } before processing and put it together again:
sed 's/{\|}/\n/g' input | sed 'main stuff' | sed ':a $!{N;ba}; s/\n\(}\|{\)\n/\1/g'
Another option is the opposite:
cat input | tr '\n' '#' | sed 'whatever; s/#/\n/g'
Both of them are ugly, mainly because operations are not limited to one team. The second is even worse, because you need to use some character or substring as a "newline holder" if it is not present in the source text.
So the question is: are there any better ways or can the above mentioned be optimized? This is a fairly regular task from what I read in recent SO questions, so I would like to choose the best practice once and for all.
PS I am mainly interested in pure sed solutions: can work work with a single sed call and nothing more? Please, not awk , Perl , etc .: this is more of a theoretical question, rather than "needing to get the job done as soon as possible."