1752796 Members
5982 Online
108789 Solutions
New Discussion юеВ

awk help

 
Chris Frangandonis
Regular Advisor

awk help

Hi all,

Is there a way that I can print $2 $4 $6 ...etc
if field $2 is -eq 2 && $6 = 6 then print $2 $4 $8.
eg
cat file | awk {if($2~/[2,3,5]1){print $0}} , will result in

BIL=84 3500 0c0000 31 49 1172850950 P=64 03030500251a 40 201c00 P=65 07 28505000 P=6e 06 4a54d53d P
=67 110000 P=7a 0100 P=6c 5244474c4142 0600 1b
BIL=84 3f00 0c0000 31 49 1148940550 P=64 03030500252e 40 201c00 P=65 0a 0124839026 P=8e 05 030000 P=6
e 06 6e39412a P=67 6f0000 P=7a 0200 P=6c 4a474d484142 0800 07 P=66 04 00 00
BIL=84 3f00 0c0000 31 49 1148940030 P=64 030305002536 40 201c00 P=65 0a 0124839028 P=8e 05 030000 P=6
e 06 8a64a712 P=67 6e0000 P=7a 0200 P=6c 4a4446484142 2f00 1c P=66 04 00 00
BIL=84 3b00 0c0000 51 49 1148307170 P=64 030305002609 40 160d00 P=65 0a 0860007249 P=82 05 1000 00 P=
6e 06 4ad2db3d P=67 080000 P=7a 0100 P=6c 4842574c4142 0900 1d

What I want to achieve is to print only the fields that I request for eg
1172850950 03030500251a 28505000 201c00 4a54d53d
1148940550 03030500252e 0124839026 201c00 6e39412a
???etc

Many Thanks
Chris
10 REPLIES 10
Carlos Fernandez Riera
Honored Contributor

Re: awk help

awk ' $2 == 2 && $6 == 6 {print $2, $4, $8}' file
unsupported
Carlos Fernandez Riera
Honored Contributor

Re: awk help

After see the example ...

awk use $2 too to field #2 and $6 to field #6

so you like to something like:

awk ' $2 == sh_var2 && $6 == sh_var6 { print $2 $4 $8 }' sh_var2=$2 sh_var6=$6 file

??????

Put a example of your file, values for search and output expected.
unsupported
Chris Frangandonis
Regular Advisor

Re: awk help

Hi Carlos,

Thanks. If you look at the file after I printed it you will see that "1172850950" is $6
and 03030500251a is $8, 28505000 is $13,"201c00" is $10 and this is were my problem lays is the next $variable. It is sometimes at $16, $19 and $20 ..etc, (common is "P=6e")

Thanks Once again
Chris
Carlos Fernandez Riera
Honored Contributor

Re: awk help

I cant understand (yet).

Maybe you need to use NF variable or/and for

awk '{ print NF , $NF , $(NF-3) }' file

will print #fields last field (last_field)-3


you can write something like

{ for ( f=20; f
( if == "P=6e" ) print $(f+1)
}

Good luck.
unsupported
Chris Frangandonis
Regular Advisor

Re: awk help

Hi Carlos,

My Output file looks like this :

BIL=84 3500 0c0000 31 49 1172850950 P=64 03030500251a 40 201c00 P=65 07 28505000 P=6e 06 4a54d53d P=67 110000 P=7a 0100 P=6c 5244474c4142 0600 1b

BIL=84 3f00 0c0000 31 49 1148940550 P=64 03030500252e 40 201c00 P=65 0a 0124839026 P=8e 05 030000 P=6e 06 6e39412a P=67 6f0000 P=7a 0200 P=6c 4a474d484142 0800 07 P=66 04 00 00
...
...

From the above example, my Output must look like this:

1172850950 03030500251a 28505000 201c00 4a54d53d
1148940550 03030500252e 0124839026 201c00 6e39412a

Hope this is of help

Chris

Volker Borowski
Honored Contributor

Re: awk help

Trying to understand:

BIL= $8 ... P=64 print_this ... P=6e print_this_also ...

So from each line starting with "BIL=" you want to print field #8 and the field following the token "P=64" and the field following the token "P=6e"

And you can not predict, if in the ... spaces are zero or more other tokens in between ?

Is the sequence of P=64 and P=6e predictable, or can P=6e be switched with P=64 ?

Volker
Chris Frangandonis
Regular Advisor

Re: awk help

Hi Vol

Yes the record starts at BIL=84 and yes I want to print #6 ,#8 ,#13, #10 , which are aligned which is no problem, but when it comes to "P=6e" ,it is sometimes at #16 or #19 or #20 depending on the bytes before P=6e
for example

BIL=84 .. .. .. .. . P=65 0a 0124839028 P=8e 05 030000 P=6e 06 8a64a712 ( <---this is #19)
BIL=84 .. ... ... .. P=65 0a 0860007249 P=82 05 1000 00 P=6e 06 4ad2db3d (<---this is #20)
and
BIL=84 .. .. .. .. . P=64 03030500251a 40 201c00 P=65 07 28505000 P=6e 06 4a54d53d (<---this is #16)

Does this help

Chris


Rodney Hills
Honored Contributor

Re: awk help

It looks like the values you are looking for are relative to the P= values. Here is a one line perl command to generate the results-

perl -n -e 'chomp; /^BIL=(\S+\s+){5,5}(\S+)/ && do {$a1=$2};/P=64 (\S+)\s+\S+\s+(\S+)/ && do {$a2=$1;$a4=$2};/P=65 \S+\s+(\S+)/ && d
o {$a3=$1};/P=6e \S+\s+(\S+)/ && do {$a5=$1};print join(" ",$a1,$a2,$a3,$a4,$a5),"\n";' yourfile

HTH

-- Rod Hills
There be dragons...
Sridhar Bhaskarla
Honored Contributor

Re: awk help

Hi Chris,

A combination of shell and awk. This will print the output in the format you wanted, except for the 10th and 13th field. Once the output is generated, you can easily use another simpl e awk statement to put them in order. Modify the DATA variable with your file

//
#!/usr/bin/ksh
DATA=data
grep "^BIL" $DATA |while read line
do
echo $line |awk '
{
if ( $0 ~ "^BIL" )
{
for (e=1; e <= NF; e++)
{
if ( $e ~ "P\=6e" ) {
printf ("%s ", $(e+2))
exit }
else
{
if ( e == 6 || e == 8 || e == 10 || e == 13 ) {
printf ("%s ", $e)}
}

}
}
}'
printf "\n"

done
//

-Sri
You may be disappointed if you fail, but you are doomed if you don't try