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?
Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.Visit Stack Exchange
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.Sign up to join this community
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
for more interesting pattern matching capability of GREP.
#!/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