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

alwbtc
  • 229
  • 1
  • 4
  • 7

2 Answers2

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.

HeatfanJohn
  • 366
  • 5
  • 14
kaji
  • 2,510
  • 16
  • 17
-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
Alexander Tolkachev
  • 4,513
  • 3
  • 14
  • 23
Raj s
  • 1