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
Gid034:s9823,s1290,s9034,s1230
Gid309:s9034,s5678,s1293,s4590
Gid124:s2145,s9008,s2381,s0234
Gid213:s9012,s9034,s8913,s9063
Required:
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 '
BEGIN{FS="[:,,]"}
{ 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}'
o/p:
x b
1 comment:
perl -wMstrict -Mvars=\$SID -slne '
my($idx, $seen);
while (/(?<=[:,])(.+?)(?=,|$)/g ) {
++$idx;
++$seen,last if $1 eq $SID;
}
print "GID=", /^([^:]+)/, "(line no $.)", "Rank $idx"
if $seen;
' -- -SID="s9034" file.txt
Post a Comment