I want to search for a string in a file, and the output should give me the line number and line containing that string.
How can I do it?
Use grep with the -n flag. Note that line numbers are 1-indexed.
grep
-n
You can use grep for this.
grep -n yourString yourFile
The general tool for searching in text files is grep. To run it as you need, just try
grep -n -F "my string" files
The -F tells grep to search for exact matches of the string. Otherwise, grep searches for regular expressions.
-F
-n tells grep to return line numbers as well.
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
By posting your answer, you agree to the privacy policy and terms of service.
tagged
asked
11 months ago
viewed
174 times
active
10 months ago