Let's say we want to do some substitutions only between some patterns, let them be <a>
and </a>
for clarity... (all right, all right, they're start
and end
!.. Jeez!)
So I know what to do if start
and end
always occur on the same line: just design a proper regex.
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 end
and I'm also OK with applying all the commands in the line containing start
before start
: just specify the address range as /start/,/end/
.
This, however, doesn't sound very useful. What if I need to do a smarter job, for instance, introduce changes inside a {...}
block?
One thing I can think of is breaking the input on {
and }
before processing and putting it back together afterwards:
sed 's/{|}/
/g' input | sed 'main stuff' | sed ':a $!{N;ba}; s/
(}|{)
/1/g'
Another option is the opposite:
cat input | tr '
' '#' | sed 'whatever; s/#/
/g'
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.
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.S. I'm mostly interested in pure sed
solutions: can the job be do with one invocation of sed
and nothing else? Please no awk
, Perl
, etc.: this is more of a theoretical question, not a "need the job done asap" one.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…