From: mathieu on 27 May 2010 10:14 Hi there, I am trying to replace a multi-line expression in a text file without much success so far. I am looking to replace "string1\nstring2" with "string3\nstring4". For this I tried: cat input.dump | sed -e '{ N s/^string1\nstring2$/string3\nstring4/ }' > output.dump But depending whether string1 is on odd or even line, sed works or not. I then tried with awk: cat input.dump | awk -v RS= 'sub(/ string1\nstring2/,"string3\nstring4")' > output.dump But this one simply gives an empty output file. I did check that string1 was present in the file. Are there any issue when string1 contains space characters ? Thanks !
From: Ben Bacarisse on 27 May 2010 10:28 mathieu <mathieu.malaterre(a)gmail.com> writes: > I am trying to replace a multi-line expression in a text file > without much success so far. I am looking to replace > "string1\nstring2" with "string3\nstring4". For this I tried: > > cat input.dump | sed -e '{ > N > s/^string1\nstring2$/string3\nstring4/ > }' > output.dump > > But depending whether string1 is on odd or even line, sed works or > not. I think you want: sed -e '/^string1$/N' -e 's/^string1\nstring2$/string3\nstring4/' <snip> -- Ben.
From: mathieu on 27 May 2010 11:18 On May 27, 4:28 pm, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote: > mathieu <mathieu.malate...(a)gmail.com> writes: > > I am trying to replace a multi-line expression in a text file > > without much success so far. I am looking to replace > > "string1\nstring2" with "string3\nstring4". For this I tried: > > > cat input.dump | sed -e '{ > > N > > s/^string1\nstring2$/string3\nstring4/ > > }' > output.dump > > > But depending whether string1 is on odd or even line, sed works or > > not. > > I think you want: > > sed -e '/^string1$/N' -e 's/^string1\nstring2$/string3\nstring4/' > > <snip> It works but I do not understand where this thing is coming from... thanks anyway you saved me quite some time !
From: pk on 27 May 2010 11:16 Ben Bacarisse wrote: > mathieu <mathieu.malaterre(a)gmail.com> writes: > >> I am trying to replace a multi-line expression in a text file >> without much success so far. I am looking to replace >> "string1\nstring2" with "string3\nstring4". For this I tried: >> >> cat input.dump | sed -e '{ >> N >> s/^string1\nstring2$/string3\nstring4/ >> }' > output.dump >> >> But depending whether string1 is on odd or even line, sed works or >> not. > > I think you want: > > sed -e '/^string1$/N' -e 's/^string1\nstring2$/string3\nstring4/' (nitpick) That has a slight issue for inputs like string1 string1 string2 etc. so the canonical way, as per the sed FAQ is sed -e :a -e '$!N;s/^string1\nstring2$/string3\nstring4/;ta' -e 'P;D' and, since \n in the rhs isn't supported by all seds, it should then be sed -e :a -e '$!N;s/^string1\nstring2$/string3\ string4/;ta' -e 'P;D'
From: Ed Morton on 27 May 2010 15:02 On May 27, 9:14 am, mathieu <mathieu.malate...(a)gmail.com> wrote: > Hi there, > > I am trying to replace a multi-line expression in a text file > without much success so far. I am looking to replace > "string1\nstring2" with "string3\nstring4". For this I tried: > > cat input.dump | sed -e '{ > N > s/^string1\nstring2$/string3\nstring4/ > > }' > output.dump > > But depending whether string1 is on odd or even line, sed works or > not. sed is an excellent tool for simple substitutions on a single line. For anything else use awk, perl, etc. > I then tried with awk: > > cat input.dump | awk -v RS= 'sub(/ > string1\nstring2/,"string3\nstring4")' > output.dump > > But this one simply gives an empty output file. I did check that > string1 was present in the file. Did you check that string2 was present on the line after it? The only way the above would fail to produce any output would be if there was no ocurrence of "string1\nstring2" in your input file. If you want every line printed whether there's a match or not (and get rid of the UUOC) you should change your script to: awk -v RS= '{sub(/string1\nstring2/,"string3\nstring4")}1' input.dump > output.dump you could also add -v ORS='\n\n' to preserve blank lines between records if they exist. Note that the above assumes string1 and string2 don't contain RE metacharacters or if they do you don't care. Ed. > > Are there any issue when string1 contains space characters ? > > Thanks !
|
Next
|
Last
Pages: 1 2 3 Prev: compare 10th column from file1 to file2 Next: aborting a bash script with control-c |