Printing the line number of first column found

Hello,

I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found.

For example, if my data has four columns:
115 34 8 0
85 112 97 0
147 77 70 0
136 35 9 0
130 31 9 0
171 120 64 1
180 141 78 1
135 87 119 1
97 130 80 1
135 177 89 1

I want to scan the file till I get to the line which has '1' in the fourth column and then print out the line number at that point, and then exit. This is what I have so far:

 awk -F " " '{if($4 == "1") print NR}' filename

Is there a way to do this without using awk as well?

Thanks!

awk '$4==1 {print NR; exit(0)}' filename

Thanks :slight_smile: But is there a way to do this without using awk?

$ i=0;while read a b c d; do i=$(expr $i + 1) && [ "$d" -eq "1" ] && echo $i && break;done < input.txt                                               
6

---------- Post updated at 11:54 AM ---------- Previous update was at 11:51 AM ----------

$ perl -lane 'if ($F[3]==1){print $.;exit}' input.txt
6