Swap lines using sed

Hi,

I have to swap two consecutive line using sed in a file.

My text to swap is available in the file x.pl

#Create & map a common work library
if (!(-e "../work"))
{
 system ("vlib work ../work");
 system ("vmap work ../work");
}
system ("vsimsa -do thiagu_dec.do");

In this i want to swap the line

system ("vmap work ../work");
}

ie after swapping it should update in the file as

}
system ("vmap work ../work");

I need this updation in many files in a directory.

Please help me

Hi,

Try:

sed '/system.*vmap work/ { N ; s/ *// ; s/\(.*\)\n\(.*\)/\2\n\1/ }' infile

Regards,
Birei

Hi birei,

Thanks for the response.
Iam getting an error like this :

sed: -e expression #2, char 61: Unknown option to 's

Thanks,
Anand.D

Hi,

Can't see the problem, but it works in my system, sorry. You could try with 'awk' instead. I hope this works:

 awk '/system.*vmap/ { getline s; printf "%s\n%s\n", s, gensub(/^ */, "", 1); next } 1' infile

Regards,
Birei

Probably the sed solution in #2 requires a semicolon before the }

Alternatively:

sed -n '/vmap work/{h;n;G;p;d;};p' infile

Hi Birei,

The awk code is working for me :).

Thanks for your help.

Can you help me by reffering a good document for awk.

Thanks,
Anand

Hi,

1.- UNIX man pages : gawk ()
2.- The GNU Awk User's Guide

Regards,
Birei

---------- Post updated at 03:45 PM ---------- Previous update was at 03:42 PM ----------

Both solutions work in my system. Perhaps adharmalingam should try it.

Regards,
Birei