|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 . |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 ' |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Yes, pure shell:
while read L;do case "${L:0:1}" in n)n=$L;;a)echo $n/${L#* };;*)echo $L;esac done<file This is ok for bash and ... 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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Yes, pure shell:
while read L;do case "${L:0:1}" in n)n=$L;;a)echo $n/${L#* };;*)echo $L;esac done<file 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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Yes, pure shell:
while read L;do case "${L:0:1}" in n)n=$L;;a)echo $n/${L#* };;*)echo $L;esac done<file |
|
![]() |
| Outils de la discussion | |
|
|