12

In Linux, How do I display lines that contain a string in a text file, such as:

search "my string" file_name 

How do I make the search case sensitive/insensitive? And how do I also display the line numbers?

Regards

2 Answers 2

23

well

grep -n "my string" file_name 

will do for your particular query. GREP is by default case sensitive in nature and to make it case insensitive you can add -i option to it. The -n option displays the line numbers. For other myriad options, I recommend

man grep 

for more interesting pattern matching capability of GREP.

0
-1
#!/bin/bash cd $HOME/Desktop s=xda m=$(grep -n "$s" $HOME/Desktop/tt.txt ) if [ "$m" = "$s" ] ;then echo "success" else echo "fail" fi 
2
  • 1
    Looks like this is wrong answer. Commented Mar 29, 2018 at 11:19
  • 1
    Also, single letter variable names are bad. This code snippet would be much more readable with better naming. Commented Mar 29, 2018 at 16:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.