From: aioe on 27 Apr 2010 10:51 Is there an easy way to replace the *previous* newline when a line contains a regexp match? I suspect awk is the right tool for this, but I'm not very familiar with awk. It appears to be possible, but very clumsy, to do this with sed. I know how to do it with an ed script, but I would like to pipeline.
From: pk on 27 Apr 2010 10:59 aioe wrote: > Is there an easy way to replace the *previous* newline when a line > contains a regexp match? I suspect awk is the right tool for this, but > I'm not very familiar with awk. It appears to be possible, but very > clumsy, to do this with sed. I know how to do it with an ed script, but > I would like to pipeline. Assuming your text does not appear on the very first line, with awk you can try this: awk '/pattern/{p="new text"}{print p}{p=$0}END{print p}' file With sed it's not so difficult (GNU sed syntax): sed -n '/pattern/{x;s/.*/new text/;x;};x;p;x;h;$p' file I can't test with ed now but I expect it to be quite simple too.
From: Janis Papanagnou on 27 Apr 2010 11:07 aioe schrieb: > Is there an easy way to replace the *previous* newline when a line > contains a regexp match? You may use printf for output and prepend '\n' to subsequent lines in case doesn't match. NR>1 && !/regexp/ { print "" } { printf "%s", $0 } END { print "" } Janis > I suspect awk is the right tool for this, but > I'm not very familiar with awk. It appears to be possible, but very > clumsy, to do this with sed. I know how to do it with an ed script, but > I would like to pipeline.
From: aioe on 27 Apr 2010 11:38 On 4/27/2010 8:59 AM, pk wrote: > aioe wrote: > >> Is there an easy way to replace the *previous* newline when a line >> contains a regexp match? I suspect awk is the right tool for this, but >> I'm not very familiar with awk. It appears to be possible, but very >> clumsy, to do this with sed. I know how to do it with an ed script, but >> I would like to pipeline. > > Assuming your text does not appear on the very first line, with awk you can > try this: > > awk '/pattern/{p="new text"}{print p}{p=$0}END{print p}' file > > With sed it's not so difficult (GNU sed syntax): > > sed -n '/pattern/{x;s/.*/new text/;x;};x;p;x;h;$p' file > > I can't test with ed now but I expect it to be quite simple too. > Thanks. I didn't make myself clear: I want to replace only the newline (with space, comma, tab or something) and not the whole previous line. For example, I would like to join the first two lines by replacing the newline that precedes [ with a space, but make no other changes, in the following example: TCP dell03:4407 mx02.eternal-september.org:nntp ESTABLISHED 2596 [thunderbird.exe] TCP dell03:netbios-ssn 192.168.2.102:51042 TIME_WAIT 0 TCP dell03:netbios-ssn 192.168.2.102:51041 TIME_WAIT 0
From: aioe on 27 Apr 2010 12:06
On 4/27/2010 9:07 AM, Janis Papanagnou wrote: > NR>1 && !/regexp/ { print "" } > { printf "%s", $0 } > END { print "" } Thanks. That works, though I would have to spend some time with my awk book to figure out why. Suppose I wanted to replace the deleted newlines with tabs? |