1

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?

0

3 Answers 3

7

Use grep with the -n flag. Note that line numbers are 1-indexed.

0

You can use grep for this.

grep -n yourString yourFile
1
  • While some questions benefit from multiple perspectives or different proposed solutions, this answer seems to add very little not already covered by soandos' answer from several months prior. Is there some way you can edit your answer to differentiate it from the earlier answer?
    – user
    Sep 1, 2014 at 14:18
0

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.

-n tells grep to return line numbers as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .