Capture specific fields in file

Dear Friends,

I have a file

a.txt

1|3478.12|487|4578.04|4505.5478|rhfj|rehtire|rhj

I want to get the field numbers which have decimal values

output:

Fields: 2,4,5

Plz help

awk -F "|" '{ for(i=1;i<=NF;i++){if($i ~ /[0-9]\.[0-9]/){s=s?s","i:i}}print s;s=""}' file
1 Like
perl -F'[|]' -ane 'print "Fields with periods: ";$n=0;
for (@F){$n++;print "$n " if /\d[.]\d/};print "\n"' file

Thanks for the reply pamu. Could you please explain the above part ?

s=s?s","i:i # Here we assign values to s. First here it checks s presents or not

s? --> Is s present? If present then do the true part else do the false part. see below.

s?True:False

Assume at start s="" so s? is false because s is not present. so it assing i to s.
Next time s? is true so it appends ","i to the s.

print s;s=""}' # Print s and set s="" means NULL.

Hope this helps you:)

1 Like

Simply superb Pamu.. Thank you so much :slight_smile:

@Elixir: Thanks for the reply. It works as Expected.:slight_smile:

May be it will be simple

grep '[0-9]\.[0-9]' test.txt