How to find lines containing a string in linux

LinuxStringFileSearch

Linux Problem Overview


I have a file in Linux, I would like to diplay lines which contain a specific string in that file, how to do this?

Linux Solutions


Solution 1 - Linux

The usual way to do this is with grep, which uses a [tag:regex] pattern to match lines:

grep 'pattern' file

Each line which matches the pattern will be output. If you want to search for fixed strings only, use grep -F 'pattern' file.

Solution 2 - Linux

Besides grep, you can also use other utilities such as awk or sed

Here is a few examples. Let say you want to search for a string is in the file named GPL.

Your sample file

user@linux:~$ cat -n GPL 
     1    The GNU General Public License is a free, copyleft license for
     2    The licenses for most software and other practical works are designed
     3  the GNU General Public License is intended to guarantee your freedom to
     4  GNU General Public License for most of our software;
user@linux:~$ 

1. grep

user@linux:~$ grep is GPL 
  The GNU General Public License is a free, copyleft license for
the GNU General Public License is intended to guarantee your freedom to
user@linux:~$ 

2. awk

user@linux:~$ awk /is/ GPL 
  The GNU General Public License is a free, copyleft license for
the GNU General Public License is intended to guarantee your freedom to
user@linux:~$ 

3. sed

user@linux:~$ sed -n '/is/p' GPL
  The GNU General Public License is a free, copyleft license for
the GNU General Public License is intended to guarantee your freedom to
user@linux:~$ 

Hope this helps

Solution 3 - Linux

/tmp/myfile

first line text
wanted text
other text

the command

$ grep -n "wanted text" /tmp/myfile | awk -F  ":" '{print $1}'
2

The -n switch to grep prepends any matched line with the line number (followed by :), while the second command uses the colon as a column separator (-F ":") and prints out the first column of any line. The final result is the list of line numbers with a match.

Solution 4 - Linux

The grep family of commands (incl egrep, fgrep) is the usual solution for this.

$ grep pattern filename

If you're searching source code, then ack may be a better bet. It'll search subdirectories automatically and avoid files you'd normally not search (objects, SCM directories etc.)

Solution 5 - Linux

Write the queue job information in long format to text file

qstat -f > queue.txt

Grep job names

grep 'Job_Name' queue.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalwbtcView Question on Stackoverflow
Solution 1 - LinuxknittlView Answer on Stackoverflow
Solution 2 - Linuxuser9013730View Answer on Stackoverflow
Solution 3 - LinuxdeFreitasView Answer on Stackoverflow
Solution 4 - LinuxBrian AgnewView Answer on Stackoverflow
Solution 5 - LinuxFVDView Answer on Stackoverflow