Swap lines using sed


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Swap lines using sed
# 1  
Old 10-29-2010
Network 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

Code:
#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

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

ie after swapping it should update in the file as

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

I need this updation in many files in a directory.

Please help me

Last edited by pludi; 10-29-2010 at 04:37 AM..
# 2  
Old 10-29-2010
Hi,

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

Regards,
Birei
# 3  
Old 10-29-2010
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
# 4  
Old 10-29-2010
Hi,

Can't see the problem, but it works in my system, sorry. You could try with 'awk' instead. I hope this works:
Code:
 awk '/system.*vmap/ { getline s; printf "%s\n%s\n", s, gensub(/^ */, "", 1); next } 1' infile

Regards,
Birei
# 5  
Old 10-29-2010
Probably the sed solution in #2 requires a semicolon before the }

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

# 6  
Old 10-29-2010
Hi Birei,

The awk code is working for me Smilie.

Thanks for your help.

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

Thanks,
Anand
# 7  
Old 10-29-2010
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 ----------

Quote:
Originally Posted by Scrutinizer
Probably the sed solution in #2 requires a semicolon before the }
Both solutions work in my system. Perhaps adharmalingam should try it.

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

Swap words using sed

Hi. I have to swap the first and the third word in all lines of a txt file using sed. Separators between words are: any charachter, except intervall. I hope, you'll understand what I want to do. my english is not so good, sorry for that:) (10 Replies)
Discussion started by: T720
10 Replies

3. Shell Programming and Scripting

Search and swap multiple lines in file using Perl

Hi all, I have a vcd file with a bunch of lines containing an array, like this $var wire 1 b a $end $var wire 1 c a $end $var wire 1 d a $end $var wire 1 e a $end $var wire 1 f b $end $var wire 1 g b $end $var wire 1 h b $end $var wire 1 i b $end I want it like this: $var wire 1 e a... (12 Replies)
Discussion started by: veerabahu
12 Replies

4. Shell Programming and Scripting

sed swap lines

Hi, I think it is possible with sed, but I'm not sure... I've a file that contains some text and filenames: gtk-media-pause | CB60471-05 - Gilbert, Brantley - Country Must Be Country Wide.zip | 8175 | /home/floris/Muziek/Karaoke/1341838939/CB60471-05 - Gilbert, Brantley - Country Must Be... (2 Replies)
Discussion started by: jkfloris
2 Replies

5. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

6. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

7. Shell Programming and Scripting

BASH: Swap first two lines in sets of 4

Hi. I may have mentioned in the OP to this thread that the AHK macro script I was trying to sort was, relative to its full length, only partly in the right format to be sorted by the methods discussed in that thread. Now I'm looking to tackle the rest of the data in that AHK script. ... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

8. Shell Programming and Scripting

How to swap order of pairs of lines?

This seems to be a question whose answer uses sed or awk. For a file like: a b c d e How to swap the order of the line pairs, to end up with: b a d c e All lines from the original file need to wind up in the output file. (8 Replies)
Discussion started by: rd5817
8 Replies

9. HP-UX

Swap device file and swap sapce

Hi I have an integrity machine rx7620 and rx8640 running hp-ux 11.31. I'm planning to fine tune the system: - I would like to know when does the memory swap space spill over to the device swap space? - And how much % of memory swap utilization should be specified (swap space device... (6 Replies)
Discussion started by: lamoul
6 Replies

10. Shell Programming and Scripting

swap words in a line with sed

Hello. There is something I can not manage : I want to swap the first word with the third one in every line. No file is given the input is read from the keyboard. I know I have to use sed, but it seems this is too complicated for me. Could you help me please ? Thanks, atticus (9 Replies)
Discussion started by: atticus
9 Replies
Login or Register to Ask a Question