Linux grep line by line

How to find lines containing a string in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

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

5 Answers 5

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

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

addition grep -rn ‘string’ /path/ if you want to search a string in a folder in which file including and line number

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

$ 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; 
$ 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 
$ 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 
$ 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 

Источник

Читайте также:  Create ftp server linux

grep: show lines surrounding each match

@StvnW . I don’t know whether to call that meta (in a more general, rather than SO context), or what to call it. You answered the question by showing how to use the answer to find the answer.

13 Answers 13

For BSD or GNU grep you can use -B num to set how many lines before the match and -A num for the number of lines after the match.

grep -B 3 -A 2 foo README.txt 

If you want the same number of lines before and after you can use -C num .

This will show 3 lines before and 3 lines after.

It is good but unfortunately the Solaris grep does not support that. See that link for solaris: unix.com/solaris/33533-grep-display-few-lines-before-after.html

Ok, but what if want to show all lines of output after the match? grep -A0 and grep -A-1 don’t cut it.

If you are HP-UX env, none of the grep versions will work like in Solaris. Was able to use the Solaris link but replace nawk with awk in that link.

-n is for line numbers, but for some versions of grep -n# will show # surrounding lines (like -c) with line numbers. That’s a useful shortcut that’s my go-to when I need context.

-A and -B will work, as will -C n (for n lines of context), or just -n (for n lines of context. as long as n is 1 to 9).

@DeepakMahakale This is probably related to how command-line arguments / options are typically parsed by POSIX programs. The option specifier is a single character (such as -A , -B or -C ). Usually, the option specifier is followed by a value ( -o a.out to specify output file in GCC), but it can also function as a simple switch / flag ( -g to enable debugging info in GCC). However spaces between options are optional, so for options without a value, it is possible to merge them ( -ABC ), which means that -15 is interpreted -1 -5 (two separate options) and the -5 overrides the -1 .

Читайте также:  Intellij idea toolbox linux

-5 is quicker than both -A 5 -B 5. Those are not meant to be used together. It is cleaner to other readers of the script if you choose -A or -B or -C over -9.

grep astring myfile -A 5 -B 5 

That will grep «myfile» for «astring», and show 5 lines before and after each match

ripgrep

If you care about the performance, use ripgrep which has similar syntax to grep , e.g.

-C , —context NUM — Show NUM lines before and after each match.

There are also parameters such as -A / —after-context and -B / —before-context .

The tool is built on top of Rust’s regex engine which makes it very efficient on the large data.

grep searchstring file -C n # n for number of lines of context up and down 

Many of the tools like grep also have really great man files too. I find myself referring to grep’s man page a lot because there is so much you can do with it.

Many GNU tools also have an info page that may have more useful information in addition to the man page.

$ grep --help | grep -i context Context control: -B, --before-context=NUM print NUM lines of leading context -A, --after-context=NUM print NUM lines of trailing context -C, --context=NUM print NUM lines of output context -NUM same as --context=NUM 

Did you not read the accepted answer? You are just repeating what has already been said on a question almost 10 years old.

Oh I’m sorry Yokai. But I don’t read anything about grepping the help section of grep to retrieve the answer.

Читайте также:  Linux mint x32 bit

If you search code often, AG the silver searcher is much more efficient (ie faster) than grep.

You show context lines by using the -C option.

ag -C 3 "foo" myFile line 1 line 2 line 3 line that has "foo" line 5 line 6 line 7 

On what system (incl. distribution name and version) did you try it? Was it installed by default? If not, what are some installation instructions? It is not installed by default on Ubuntu MATE 20.04 (Focal Fossa).

Let’s understand using an example.
We can use grep with options:

-A 5 # this will give you 5 lines after searched string. -B 5 # this will give you 5 lines before searched string. -C 5 # this will give you 5 lines before & after searched string 

Example. File.txt contains 6 lines and following are the operations.

[abc@xyz]~/% cat file.txt # print all file data this is first line this is 2nd line this is 3rd line this is 4th line this is 5th line this is 6th line [abc@xyz]~% grep "3rd" file.txt # we are searching for keyword '3rd' in the file this is 3rd line [abc@xyz]~% grep -A 2 "3rd" file.txt # print 2 lines after finding the searched string this is 3rd line this is 4th line this is 5th line [abc@xyz]~% grep -B 2 "3rd" file.txt # Print 2 lines before the search string. this is first line this is 2nd line this is 3rd line [abc@xyz]~% grep -C 2 "3rd" file.txt # print 2 line before and 2 line after the searched string this is first line this is 2nd line this is 3rd line this is 4th line this is 5th line 

Trick to remember options:

  1. -A → A means «After»
  2. -B → B means «Before»
  3. -C → C means «in between»

Источник

Оцените статью
Adblock
detector