How to return the line number of the first occurrence of a text in a file under Linux

advertisements

How to return the line number of the first occurrence of a text in a file in linux? For instance, a file as follows.

12:04:56 xxxx
12:06:23 xxxx
12:09:11 xxxx
12:09:13 xxxx
12:10:12 xxxx

Can anyone provide a line command which returns 3 if the desire text is "12:09:"? Thanks.


Try this :

awk '/12:06:23/{print NR;exit}' file

and with grep and cut :

grep -n -m1 "12:06:23" file | cut -d':' -f1