|
From: Mr_Noob on 23 Apr 2008 04:01 Hi all, here is an extract of my file : .... name Paul age 34 sex male phone 999333 name Tom age 26 sex male phone 444999 name Lisa age 42 sex female phone 222777 My aim is to have a shell script that would grab the "age" element and stick it right after the "name" element (located on the line above...) Here is a vision of the output i want to generate : name Paul/34 sex male phone 999333 name Tom/26 sex male phone 444999 name Lisa/42 sex female phone 222777 any idea??? Cheers
From: Dave B on 23 Apr 2008 05:20 On Wednesday 23 April 2008 10:01, Mr_Noob wrote: > Hi all, > > here is an extract of my file : > > ... > name Paul > age 34 > sex male > phone 999333 > name Tom > age 26 > sex male > phone 444999 > name Lisa > age 42 > sex female > phone 222777 > > > My aim is to have a shell script that would grab the "age" element and > stick it right after the "name" element (located on the line above...) > > Here is a vision of the output i want to generate : > > name Paul/34 > sex male > phone 999333 > name Tom/26 > sex male > phone 444999 > name Lisa/42 > sex female > phone 222777 Try this: sed '/^name/ {N;s%\nage %/%}' yourfile This assumes that the "age" line always follows immediately the "name" line. If that is not the case, then an awk solution is the next candidate. -- D.
From: Ed Morton on 23 Apr 2008 06:59 On 4/23/2008 3:01 AM, Mr_Noob wrote: > Hi all, > > here is an extract of my file : > > ... > name Paul > age 34 > sex male > phone 999333 > name Tom > age 26 > sex male > phone 444999 > name Lisa > age 42 > sex female > phone 222777 > > > My aim is to have a shell script that would grab the "age" element and > stick it right after the "name" element (located on the line above...) > > Here is a vision of the output i want to generate : > > name Paul/34 > sex male > phone 999333 > name Tom/26 > sex male > phone 444999 > name Lisa/42 > sex female > phone 222777 > > > any idea??? > > Cheers awk '/^name/{name=$0;next} /^age/{$0=name"/"$2} 1' file Before you ask - "1" is a true condition which invokes the default action of printing the current record. You can replace it with "{print $0}" or just "{print}" if you like. Ed.
From: Mr_Noob on 23 Apr 2008 08:12 perfect! The "age" line follows immediately the "name" line, but i'll go with 'awk' just in case of... Thank you very much for your help.
From: Michael Paoli on 24 Apr 2008 00:44 Mr_Noob wrote: > here is an extract of my file : > > ... > name Paul > age 34 > sex male > phone 999333 > name Tom > age 26 > sex male > phone 444999 > name Lisa > age 42 > sex female > phone 222777 > > My aim is to have a shell script that would grab the "age" element and > stick it right after the "name" element (located on the line above...) > > Here is a vision of the output i want to generate : > > name Paul/34 > sex male > phone 999333 > name Tom/26 > sex male > phone 444999 > name Lisa/42 > sex female > phone 222777 perl -e ' local $/; while(<>){ s/^(name [^\n]*)\nage ([^\n]*)/\1\/\2/gm; print; } ' The above perl approach is a bit simplistic - it sucks in all of the input file before processing it, so it will fail for sufficiently large input. The sed approach below at most stores two adjacent lines, so it will generally work with large input files - at least if the lines aren't too long. The more complex (and robust) sed logic could likewise be done in perl, or the more simplistic perl logic (using pattern or hold space) done with sed. sed -ne ' #hold space is Empty or garbage (overwrite it if we need the hold space) :e #if last line: ${ #print patern space: p #quit: q } #if not match /^name / /^name /!{ #print pattern space: p #get next line: n #branch to e: b e } #(implicit) matched /^name / and not last line #copy pattern space to hold space: h #get next line: n #fall through #Name in hold space :n #if match /^age / /^age /{ #substitute for /^age / nothing in pattern space: s/^age // #append pattern space to hold space with embedded newline: H #exchange pattern and hold spaces: x #substitute for embeded newline / in pattern space: s/\n/\// #print pattern space: p #if last line, quit: $q #get next line: n #branch to e: b e } #(implicit) Name in hold space but did not match /^age / #exchange pattern and hold spaces: x #print pattern space: p #exchange pattern and hold spaces: x #branch to e: b e '
|
Next
|
Last
Pages: 1 2 Prev: Matching Previous and Append with Sed / Awk Next: xml diff and patch in shell script: |