Manipulate lines with sed/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulate lines with sed/awk
# 1  
Old 06-29-2009
Manipulate lines with sed/awk

Hey All,

I need to reorganize a file's text. Here is the source:

host John_Doe
filename "config.cfg";
hardware ethernet 98:10:3d:13:8f:98;
fixed-address 10.10.10.29;
}
host Jane_Doe
filename "config.cfg";
hardware ethernet 98:13:11:fd:5a:57;
fixed-address 10.10.5.24;
}
host Jim_Doe
filename "config.cm";
hardware ethernet 98:10:3d:13:8c:6c;
fixed-address 10.10.7.53;
}
host Cookie_Monster
filename "config.cfg";
hardware ethernet 98:10:3d:13:8f:9e;
fixed-address 10.10.10.30;
}
host Big_Bird
filename "config.cfg";
hardware ethernet 98:13:11:fd:77:e8;
fixed-address 10.10.6.63;
}



What I need it to look like is this:

host John_Doe {hardware ethernet 98:10:3d:13:8f:98;
fixed-address 10.10.10.29;
filename "config.cfg";}
# -
# -
# -
# -
# -@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-
host Jane_Doe {hardware ethernet 98:13:11:fd:5a:57;
fixed-address 10.10.5.24;
filename "config.cfg";}
# -
# -
# -
# -
# -@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-
host Jim_Doe {hardware ethernet 98:10:3d:13:8c:6c;
fixed-address 10.10.7.53;
filename "config.cm";}
# -
# -
# -
# -
# -@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-
host Cookie_Monster {hardware ethernet 98:10:3d:13:8f:9e;
fixed-address 10.10.10.30;
filename "config.cfg";}
# -
# -
# -
# -
# -@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-
host Fred_Flintstone {hardware ethernet 98:13:11:fd:77:e8;
fixed-address 10.10.6.63;
filename "config.cfg";}
# -
# -
# -
# -
# -@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-

The part I am not able to figure out is how to get the manipulator (sed/awk) to move lines up and down in a file. I can figure out substitution and adding lines below or above certain other lines.

Could anyone give me a couple of pointers?

TIA
# 2  
Old 06-29-2009
Should be something like this:

Code:
awk 'BEGIN{s="# -\n# -\n# -\n# -\n# -@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-@@@-"}
/}/{print a[1] " {" a[3] "\n" a[4] "\n" a[2] "}\n" s; i=0; next}
{a[++i]=$0}
' file > newfile

# 3  
Old 06-29-2009
THANK YOU VERY MUCH!! That did exactly what it needed to.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

Manipulate columns using sed

Hello, I would like to remove the first column of lines beginning by a character (in my case is an open square bracket) and finishing by a space (or any other delimiter). For example: string1 string2 string3 to string2 string3 I found this previous topic: ... (1 Reply)
Discussion started by: stoyanova
1 Replies

4. Shell Programming and Scripting

sed script to manipulate the /etc/passwd file

Hi. Can anybody help me with a script to extract usernames, shell and home_directory from the /etc/passwd file. I am stuck (2 Replies)
Discussion started by: Pauline mugisha
2 Replies

5. Shell Programming and Scripting

how to manipulate with lines while playing with data

hello everyone, well I have a file which contains data, I want to add the data on hourly basis, like my file contains data for 24 hours, (so a total of 1440 ) lines. Now i want to add the data on hourly basis to get average values. like if I use (head) command it is ok for first go, but... (5 Replies)
Discussion started by: jojo123
5 Replies

6. Shell Programming and Scripting

manipulate postscript via sed

dear all, on solaris10 for x86 i am trying to modify the creation date of a postscript file with sed in a csh script. sed is driving me crazy though...i think due to the spaces in the string i am trying to substitute?? part of the postscript file: %!PS-Adobe-3.0... (3 Replies)
Discussion started by: lada niva
3 Replies

7. Shell Programming and Scripting

RE or Sed to manipulate a document

I have a file with a list of about 2500 lines that I'd like to reformat using some regex in notepad++ or sed from my box (FreeBSD 6.3-RELEASE i386) to do the following: convert from: mycommand > mydocument0.htmlbcfn mycommand > mydocument1.htmlcdcsfn mycommand > mydocument2.htmldechfn... (6 Replies)
Discussion started by: phpfreak
6 Replies

8. Shell Programming and Scripting

Sed command to find, manipulate and replace a number

Hi, Im very new to the world of sed so I'm really not even sure if this is possible. What i need to do is read from a flat file and every time i see this line: VAL=123,456 I need to change 456 to 457 for every occurence of this line in the file. The numbers 123 and 456 are different for... (6 Replies)
Discussion started by: LT_2008
6 Replies

9. UNIX for Dummies Questions & Answers

using sed to manipulate text in files

Hi, I have a slight problem in trying to manipulate the text within a file using the "sed" command in that the text i need changed has "/" slashes in. I have a .sh script that scans the "/db/sybbackup/" directories for any .dmp file older than 2 days and then to >> the information to a file called... (3 Replies)
Discussion started by: Jefferson333
3 Replies

10. Shell Programming and Scripting

sed or other tool to manipulate data, including email addresses

I have a list of names and email addresses, like this. The <tab> markers are actually tabs. joe.blow <tab> joe.blow@wherever.com tom.t.hall <tab> tom.t.hall@wherever.com john.r.smith <tab> john.r.smith@wherever.com sally.jones <tab> sally.jones@state.or.us I want to parse the data so that... (3 Replies)
Discussion started by: manouche
3 Replies
Login or Register to Ask a Question