Find field number

I have this line in the file:

,2,353867835022;11,353681041426390,272023201187741,272-02f-20017-06609,353854100352;11,,,,,,,0854100352,3,00,,O,D,DATA,,,7124395,,,17687,16,HPLMN,M20MSS_TTFILE_8377_20110528170245,M20MSS,W30B22I;0GRI3,1,20110528130013,170054,1,41,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001820,,,,b60a5c0014,1:353867835022::::0854100352::353854100352,,,,,,,, 

Yes, this is a comma "," split file. There is number 17687. I want to know how much of this field is in the row. I want to use this as a base and include a script in the shell.

+4
source share
6 answers

Field # 26:

 % awk -F',' '/17687/ { for (f = 0; f < NF; ++f) { if ($f == "17687") { print $f " found in field number " f " of " NF " on line " NR "." } } }' test.csv 17687 found in field number 26 of 75 on line 1. 

This allows you to find 17687 in multiple fields on multiple lines.

Hope this helps.

+3
source

So, do you want the number of commas to 17687? One way to do this:

 sed -r 's/(^.*,)17687,.*$/\1/;s/[^,]//g'|wc -c 

This captures everything until 17687, removes all non-commas and counts the number of characters.

Using this in a script, you can do something like:

 FIELD_NO=`sed -r 's/(^.*,)17687,.*$/\1/;s/[^,]//g'|wc -c` cut -d',' -f$FIELD_NO some_file 
+3
source

You can also use tr to change the field separator to a new line, grep to find the line and cat if you want. For instance:

 $ cat t.csv|tr ',' '\n'|cat -n|grep 17687 26 17687 

or better

 $ cat t.csv|tr ',' '\n'|grep -n 17687 26:17687 

Or even

 $ tr ',' '\n' < t.csv |grep -n 17687 26:17687 
+1
source

Perl?

 FLD="17687" perl -F/,/ -slane '%h=map{$_,++$i}@F ;print $h{$fld}||0' -- -fld="$FLD" 

for the line of your example 26 will be printed (from 1) or "0" if not found. Will search for the last row index.

or

 perl -F/,/ -slane 'map{print}grep { $F[$_] eq $fld } 0..$#F;' -- -fld="$FLD" 

prints all indexes (read from 0) or nothing ...

+1
source

A slight improvement over the version of David using only one regular expression.

 sed -r "s/17687,.*|[^,]*//g" | wc -c 
+1
source

Awk oneliner, one process:

 awk -F, '/17687/{n=NF;sub(".*,17687,","");print n-NF}' file 

For large files, use the mawk zipper, if available on your platform.

+1
source

All Articles