1

I have csv file and I want to see numbers that start with 0.99 and their row's index which is the first cell of that row.

This is what I have so far:

cat fil.csv | grep '0\\.99'| tee > (cut -d, -f1) | tr , \\n |  grep '0\\.99'

input:

id, f1,f2,f3
f1,0.54,0.12,0.432
f2,0.1231,0.99999,0.99832
f3,0.121,nan,0.12321
f4,0.99712,0.121,0.434

desired output: ideally i want this but it would be too complex for oneliner:

f2,0.99999,0.99832
f4,0.99712

I can settle for this, which is what I want from the command that I wrote:

f2
0.99999
0.99832
f4
0.99712
  • 1
    You can use just one GNU grep : grep -own '0\.99\w*' – Costas Nov 17 '14 at 16:18
1

With awk:

$ awk -F, '$0~"0\\.99*"{printf $1;for(i=1;i<=NF;i++){if($i~"0\\.99*"){printf ","$i}};printf "\n"}'
f2,0.99999,0.99832
f4,0.99712

In more readable form:

$ awk -F, '
      $0~"0\\.99*"{
          printf $1
          for(i=1; i<=NF; i++){
              if($i~"0\\.99*"){
                  printf ","$i
              }
          }
          printf "\n"
      }
  '
0

If you can use perl:

$ perl -F, -anle '
  BEGIN { $, = "," }
  @h = grep { /^0\.99/ } @F;
  print $F[0], @h if @h;
' file
f2,0.99999,0.99832
f4,0.99712

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.