Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

text

shell

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:"?

like image 376
Roy Chan Avatar asked Mar 17 '23 15:03

Roy Chan


1 Answers

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
like image 82
Gilles Quenot Avatar answered Mar 26 '23 01:03

Gilles Quenot