Swapping lines beginning with certain words using sed/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swapping lines beginning with certain words using sed/awk
# 1  
Old 08-23-2009
Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this:
Code:
fixed-address 192.168.6.6 {
  hardware ethernet 00:22:64:5b:db:b1;
  host X;
}
fixed-address 192.168.6.7 {
  hardware ethernet 00:22:64:5b:db:b3;
  host Y;
}
fixed-address 192.168.6.8 {
  hardware ethernet 00:22:64:5b:db:b4;
  host A;
}
fixed-address 192.168.6.9 {
  hardware ethernet 00:22:64:5b:db:b6;
  host B;
}

I need the file to read like:
Code:
host X {
  hardware ethernet 00:22:64:5b:db:b1;
  fixed-address 192.168.6.6;
}
host Y {
   hardware ethernet 00:22:64:5b:db:b3;
   fixed-address 192.168.6.7;
 }
host A {
   hardware ethernet 00:22:64:5b:db:b4;
   fixed-address 192.168.6.8;
 }
 host B {
   hardware ethernet 00:22:64:5b:db:b6;
   fixed-address 192.168.6.9;
 }

So there are two things that need to be done:
1. The lines beginning with
Code:
host

need to be swapped with the lines beginning with
Code:
fixed-address

2. The semi-colons at the end of the lines beginning with
Code:
host

shouldn't move up and the curly brackets at the end of the lines beginning with
Code:
fixed-address

shouldn't move either.

Thanks in advance

--
KSK
# 2  
Old 08-23-2009
One solution using awk
Code:
awk '/fixed/{y=$1FS$2;getline;x=$0;getline;sub(";","");sub("^  ","");printf "%s{\n%s\n%s;\n",$0FS,x,FS FS y;next}1' file

# 3  
Old 08-23-2009
Works! Thanks a ton.

I known very little awk and, out of curiosity, I wonder how that command works?
# 4  
Old 08-23-2009
# 5  
Old 08-23-2009
Thanks muchly Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

Count words/lines between two tags using awk

Is there an efficient awk that can count the number of lines that occur in between two tags. For instance, consider the following text: <s> Hi PP - my VBD - name DT - is NN - . SENT . </s> <s> Her PP - name VBD - is DT - the NN - same WRT - . SENT - </s> I am interested to know... (4 Replies)
Discussion started by: owwow14
4 Replies

3. Shell Programming and Scripting

AWK swapping fields on different lines

Hi All, Sorry if this question has been posted elsewhere, but I'm hoping someone can help me! Bit of an AWK newbie here, but I'm learning (slowly!) I'm trying to cobble a script together that will save me time (is there any other kind?), to swap two fields (one containing whitespace), with... (5 Replies)
Discussion started by: Bravestarr
5 Replies

4. UNIX for Advanced & Expert Users

Need help either with awk or sed to get text between words

Hello All, My requirement is to get test between two words START & END, something like html tags Eg. Input file: START Line1 Line2 Line3 CLOSE START Line4 Line5 Line6 END START Line7 START Line8 (7 Replies)
Discussion started by: konerusuneel
7 Replies

5. UNIX for Dummies Questions & Answers

Remove words beginning with a certain character from a file

Hi, how could you go about removing words that begin with a certain character. assuming that this character is '-' I currently have echo "-hello" | sed s/-/""/ which replaces the leading dash with nothing but I want to remove the whole word, even if there are multiple words beginning... (3 Replies)
Discussion started by: skinnygav
3 Replies

6. Shell Programming and Scripting

swapping lines that match a condition using sed, perl or the like

I'm a bit new to regex and sed/perl stuff, so I would like to ask for some advice. I have tried several variations of scripts I've found on the net, but can't seem to get them to work out just right. I have a file with the following information... # Host 1 host 45583 { filename... (4 Replies)
Discussion started by: TheBigAmbulance
4 Replies

7. Shell Programming and Scripting

awk help needed in trying to count lines,words and characters

Hello, i am trying to write a script file in awk which yields me the number of lines,characters and words, i checked it many many times but i am not able to find any mistake in it. Please tell me where i went wrong. BEGIN{ print "Filename Lines Words Chars\n" } { filename=filename + 1... (2 Replies)
Discussion started by: salman4u
2 Replies

8. Shell Programming and Scripting

Swapping or switching 2 lines using sed

I made a script that can swap info on two lines using a combination of awk and sed, but was hoping to consolidate the script to make it run faster. If found this script, but can't seem to get it to work in a bash shell. I keep getting the error "Too many {'s". Any help here would be appreciated:... (38 Replies)
Discussion started by: LaTortuga
38 Replies

9. UNIX for Dummies Questions & Answers

merging 2 lines with awk and stripping first two words

Hey all i am pretty new to awk... here my problem. My input is something like this: type: NSR client; name: pegasus; save set: /, /var, /part, /part/part2, /testpartition, /foo/bar,... (9 Replies)
Discussion started by: bazzed
9 Replies

10. Shell Programming and Scripting

Adding words to beginning of lines

I have a file that contains a great number of lines, let's say 183 lines, and I want to add: echo " to the beginning of each line. What is the easiest way to do it? Tx (9 Replies)
Discussion started by: Ernst
9 Replies
Login or Register to Ask a Question