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. 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.
    – chicks
    Mar 29, 2018 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.