Need help with awk

Hi Guys,
Need your help to optimized the script

I have a file which have data like this

$ cat /auto/atsingh5/outtest

                           PFLAG
-------------------------- ----- -----------
2011-06-16-08.59.38.122168 5            2050
2011-06-16-08.59.38.122168 9            2372
2011-06-16-08.59.38.122168 B             651
2011-06-16-08.59.38.122168 C            7422
2011-06-16-08.59.38.122168 D             291
2011-06-16-08.59.38.122168 P              43
2011-06-16-08.59.38.122168 S            5188
2011-06-16-08.59.38.122168 T            1085
2011-06-16-08.59.38.122168 V             691
2011-06-16-08.59.38.122168 Y           84232

  10 record(s) selected.

I need the output like this

2011-06-16-08.59.38.122168
5            2050
9            2372
B             651
C            7422
D             291
P              43
S            5188
T            1085
V             691
Y           84232

please help
Now I am using this code

awk '{print $1}' /auto/atsingh5/outtest | grep -v "^$" | tail -2 | head -1 > output
awk '{print $2,"  ", $3}' /auto/atsingh5/outtest | grep '[A-Z0-9]'>> output

Is it possible to do in one command ??

Try this. Not in awk though :frowning:

 
perl -lane '$hash{$F[0]}{$F[1]}=$F[2];END{for $k1 (keys %hash){print $k1;print "$_ $hash{$k1}{$_}" for keys %{$hash{$k1}}}}' inp
 
bash-3.00$ cat -n /tmp/myfile
     1
     2                             PFLAG
     3  -------------------------- ----- -----------
     4  2011-06-16-08.59.38.122168 5            2050
     5  2011-06-16-08.59.38.122168 9            2372
     6  2011-06-16-08.59.38.122168 B             651
     7  2011-06-16-08.59.38.122168 C            7422
     8  2011-06-16-08.59.38.122168 D             291
     9  2011-06-16-08.59.38.122168 P              43
    10  2011-06-16-08.59.38.122168 S            5188
    11  2011-06-16-08.59.38.122168 T            1085
    12  2011-06-16-08.59.38.122168 V             691
    13  2011-06-16-08.59.38.122168 Y           84232
    14
    15
bash-3.00$ nawk '{ if (NR==4) {printf ("%s\n",$1)} if(NR>=4) {print $2,$3}}' /tmp/myfile
2011-06-16-08.59.38.122168
5 2050
9 2372
B 651
C 7422
D 291
P 43
S 5188
T 1085
V 691
Y 84232
 

with tab seperated

 
nawk '{ if (NR==4) {printf ("%s\n",$1)} if(NR>=4) {print $2"\t"$3}}' /tmp/myfile

thanks guys for helping me :slight_smile:

nawk '/2011/ {print $3,$4}' filename

Thanks
Sha

gawk '/^[0-9]/{a[$1]=a[$1]"\n"$2"\t"$3}END{for (i in a) print i,a}' infile.txt

thanks ahmad , Now need another help !!

can we store these commands in an variable ?? like

a=" awk '/^[0-9]/{a[$1]=a[$1]"\n"$2"\t"$3}END{for (i in a) print i,a}'  "
b=" db2 "select * from table" "

I want to use like this

$a | $b

means I do not want to write the same command again and again .

please suggest

yes you can. But what you are trying to do here doesn't make sense. I mean output of command $a is piped to command $b which is not using that data.

regards,
Ahamed

Sorry.
I typed wrong. I want to ..

a=" awk '/^[0-9]/{a[$1]=a[$1]"\n"$2"\t"$3}END{for (i in a) print i,a}'  " 
b=" db2 "select * from table" "

$b | $a

but the problem is, when I tried this I got this error

$b | $a 

 Syntax Error The source line is 1.
 The error context is
                 >>> ' <<<
        awk: 0602-540 There is a missing } character.
 awk: 0602-500 Quitting The source line is 1.

please help..

Try:

a="awk '"'/^[0-9]/{a[$1]=a[$1]"\n"$2"\t"$3}END{for (i in a) print i,a}'"'"
b=' db2 "select * from table" '
eval "$b | $a"