Find x and print its record

Hi all,

I have a file containing two fields with 154 rows/records/lines (forgive me, my UNIX terminology is not quite up to par yet). I am trying to read from this list, find a value (lets say 0), then print the record/line/row that value falls on (In this case it would be record/line/row #27)? Here is an example of the type of file Im dealing with...

1023 311
899 311
1013 311
583 311
439 311
409 311
199 311
149 311
138 311
138 311
112 311
116 311
148 311
 82 311
 16 311
 17 311
 23 311
 14 311
  8 311
  9 311
  8 311
  4 311
 14 311
  2 311
  2 311
  2 311
  0 311
  1 311

I thought perhaps awk might do the trick, but I suspect there might be something better, and frankly, I havent a clue as to how to solve this problem. Any help would be greatly appreciated!

-SS

awk '/0/' file
awk -v x=0 '{for(i=0;++i<=NF;){if($i==x){print;next}}}' file

Hi Danmero,

Thanks for your solution, it's is really close to what I need, but...I actually need the line number that the value 0 falls on, in my example it was line #27, the value 0 is is just the criterion if you will.

This list represent points in time (in total 154) within fMRI data, the values in field $1 represent deviations from baseline within those points in time, basically head movement by the subject. The value 0 is significant because it represents no such deviations from baseline. The line number that that 0 falls on is important because it represents a specific point within the temporal sequence of the fMRI scan in which the subject wasn't moving. I take that value (the line number) and input it into another program so that it can properly register the entire fMRI dataset. To further complicat things, this 0 value will be different for every subject and every fMRI scan.

Does that help to clarify my problem a bit?

Let's try this one:

awk -v x=0 '$1==x{print NR}' file
27
1 Like

Danmero your brilliant! Thank you so much for your help! This is exactly what I needed.

This solution was a lot simpler than I expected, I am rather little embarrassed I didnt come up with this myself. All the same, I really appreciate your help.

Best regards

SS