Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

/proc/kallsyms format, sqldatasource dal, wwwxxxenden, convert raid5 to raid 10 mdadm, apache force chunked, nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas

Links

XODOX
Impressum

#1: Match a field and move data on the line above...

Posted on 2008-04-23 10:01:30 by gniagnia

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

Report this message

#2: Re: Match a field and move data on the line above...

Posted on 2008-04-23 11:20:10 by Dave B

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.

Report this message

#3: Re: Match a field and move data on the line above...

Posted on 2008-04-23 12:59:20 by Ed Morton

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.

Report this message

#4: Re: Match a field and move data on the line above...

Posted on 2008-04-23 14:12:31 by gniagnia

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.

Report this message

#5: Re: Match a field and move data on the line above...

Posted on 2008-04-24 06:44:44 by Michael Paoli

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
'

Report this message

#6: Re: Match a field and move data on the line above...

Posted on 2008-04-24 15:01:57 by mop2

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

Report this message

#7: Re: Match a field and move data on the line above...

Posted on 2008-04-24 15:06:04 by mop2

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

Report this message

#8: Re: Match a field and move data on the line above...

Posted on 2008-04-24 15:08:47 by mop2

Yes, pure shell:

while read L;do
case "${L:0:1}" in n)n=$L;;a)echo $n/${L#* };;*)echo $L;esac
done<file

Report this message