Issue
How can I use a bash script to find the line number where a string occurs?
For example if a file looked like this,
Hello I am Isaiah
This is a line of text.
This is another line of text.
and I ran the script to look for the string "line" it would output the number 2, as it is the first occurance.
Solution
Given that your example only prints the line number of the first occurrence of the string, perhaps you are looking for:
awk '/line/{ print NR; exit }' input-file
If you actually want all occurrences (eg, if the desired output of your example is actually "2\n3\n"), omit the exit
.
Answered By - William Pursell Answer Checked By - Senaida (WPSolving Volunteer)