Friday, August 14, 2009

Awk - find column number of a pattern

Input file contains the result of annual day sports meet for a class.

The format is:

gameId:1st Place Student ID,2nd Place Student ID and so on

$ cat file.txt

We need to search the above file and find out the details (Game ID, line number where the student is found and its Rank in that game) of student ID say "s9034"

The awk solution:

$ awk '
{ for(i=1;i<=NF;i++){
if ($i ~ /\<s9034\>/)
{print "GID="$1"(line no "NR")","Rank " i-1 } }
}' file.txt

output of the above script:

GID=Gid034(line no 1) Rank 3
GID=Gid309(line no 2) Rank 1
GID=Gid213(line no 4) Rank 2

Important learn from the above post:

- How to specify multiple field separator i.e. FS in AWK (read my earlier post)

Note this:

$ echo "x:a,b,c" | awk 'BEGIN{FS="[:,,]"} {print $1,$3}'


x b

1 comment:

Karan Bohra said...

perl -wMstrict -Mvars=\$SID -slne '
my($idx, $seen);
while (/(?<=[:,])(.+?)(?=,|$)/g ) {
++$seen,last if $1 eq $SID;
print "GID=", /^([^:]+)/, "(line no $.)", "Rank $idx"
if $seen;

' -- -SID="s9034" file.txt

© Jadu Saikia