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?




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.

cd $HOME/Desktop
m=$(grep -n "$s" $HOME/Desktop/tt.txt )
if [ "$m" = "$s" ] ;then
    echo "success"
  echo "fail"
  • 1
    Looks like this is wrong answer. – Alexander Tolkachev Mar 29 '18 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 '18 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.