1834829 Members
2576 Online
110070 Solutions
New Discussion

awk formatting output

 
SOLVED
Go to solution
alec pringle
Frequent Advisor

awk formatting output

Hi,

can anyone help me format the following file:

time: 20050310 field1: a field2: b b field3: c
time: 20050311 field2: a b
time: 20050311 field3: a b field2: bb

..into:

time: 20050310 field2: b b field3: c
time: 20050311 field2: a b
time: 20050311 field2: bb field3: a b


.. because the order and number of fields is not fixed I am struggling to get awk to pull the right data out - any ideas?
10 REPLIES 10
Keith Bryson
Honored Contributor

Re: awk formatting output

Alec

It's not clear what the 'rules' are for re-structuring your output. Is each line structured/cut differently?

Keith
Arse-cover at all costs
alec pringle
Frequent Advisor

Re: awk formatting output

In the output I want time, field2 and field3, but only if they exist. I don't want field1 in the output.
RAC_1
Honored Contributor

Re: awk formatting output

cat "file"|sort -k3|sort -k4|sort -k5

Anil
There is no substitute to HARDWORK
RAC_1
Honored Contributor

Re: awk formatting output

cat "file"|sed 's/field1//g'|sort -k3|sort -k4|sort -k5
There is no substitute to HARDWORK
alec pringle
Frequent Advisor

Re: awk formatting output

Thanks Anil,

I think the sed will only remove the field name (and not its data) and the sort will then not work because the number of fields will be different. Also, I cannot guarantee the position of field2 and field3 (see the final line of the example), so this means a sort won't work.

Alec
RAC_1
Honored Contributor

Re: awk formatting output

cat "file"|sed 's/field1: .//g'|sort -k3|sort -k4|sort -k5

Note - field1: . in sed command.
There is no substitute to HARDWORK
H.Merijn Brand (procura
Honored Contributor

Re: awk formatting output

unlimited number of fields:

lt09:/tmp 107 > cat xx
time: 20050310 field1: a field2: b b field3: c
time: 20050311 field2: a b
time: 20050311 field3: a b field2: bb
lt09:/tmp 108 > perl -lpe'@f=split/(\s+field\d+:)/;$i=shift@f;%f=@f;$_=join"",$i,map{"$_$f{$_}"}sort keys%f' xx
time: 20050310 field1: a field2: b b field3: c
time: 20050311 field2: a b
time: 20050311 field2: bb field3: a b
lt09:/tmp 109 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: awk formatting output

Procura,

He doesn't want "field1" included with the output.

So how about modifying your routine as follows-

perl -lpe'@f=split/\s+(field\d+:)/;$i=shift@f;%f=@f;delete $f{"field1:"};$_=join" ",$i,map{"$_$f{$_}"}sort keys%f' xx

-- Rod Hills

There be dragons...
H.Merijn Brand (procura
Honored Contributor
Solution

Re: awk formatting output

Ge didn't state that explicitely, so I assumed he just err'd on the output.

Your modification also changes the output, since I included the whitespace leading towards the fields in the separator, and you took it out. That indeed makes it easier to delete the key field1:, but it also glues the fields together in a different way. You solved that by adding the space in the join, but it still is different from my original plan.

So /my/ modification to filer field1:'s would be

# perl -lpe'@f=split/(\s+field\d+:)/;$i=shift@f;%f=@f;$_=join"",$i,map{"$_$f{$_}"}grep!/\bfield1:/,sort keys%f' xx

There is still a small weakness in there if there are more than 9 keys (1,10,11,12,2,3,4,5,6,7,8,9), which could be schwarzian'd

# perl -lpe'@f=split/(\s+field\d+:)/;$i=shift@f;%f=@f;$_=join"",$i,map{"$_$f{$_}"}grep!/\bfield1:/,map{$_->[0]}sort{$a->[1]<=>$v->[1]}map{/(\d+)/;[$_;$1]}keys%f' xx

But now we're getting towards the point where you'd normaly turn from a one-liner to a script, and document what you're doing

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
alec pringle
Frequent Advisor

Re: awk formatting output

thanks for all your help!