Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grepping multiple lines from a file

Tags:

regex

grep

I would like to grep in order multiple lines of a file. An example of the file originalfile.txt could be:

num=12
workers not specified
length= 128
Using array element 
num= 24 
workers not specified
length= 128
Using array element 
......

I want to grep only valuable lines like all those with num and length:

num=12
length= 128
num= 24 
length= 128
......

I know how to grep for just one pattern, say num but I don't know how to do it for more than one pattern.

$ grep "num" originalfile.txt

It turns out that I have some parameters in the same line that awk does not seem to find,i.e:

.... time= 1.234 Gflop/s= 3.4556 .....

It filters the first one, but not Gflop/s. Is there a way to recursvely find on the same line?

like image 842
Manolete Avatar asked Aug 10 '13 14:08

Manolete


People also ask

Can you grep multiple lines?

The problem with using grep's regular expression is that the pattern is limited to only a single line. While it's possible to use grep multiple times to achieve the required result, it's more convenient to use the -P or –perl-regexp option. The -P option enables the PCRE add-on for grep.

How do you grep multiple lines after a match?

Use the -A argument to grep to specify how many lines beyond the match to output. And use -B n to grep lines before the match. And -C in grep to add lines both above and below the match!

How do you grep 3 lines after a match?

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. 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.

How do you grep multiple things?

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path. The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.


3 Answers

Use the -E option

From man grep:

-E, --extended-regexp

Interpret PATTERN as an extended regular expression (see below).

$ grep -E 'length|num' data
num=12
length= 128
num= 24 
length= 128

Update if you want to only get the numbers you can pipe to awk

grep -E 'length|num' data | awk -F'= ?' '{print $2}'

But then you can do it all in a signle awk command, and avoid grep

awk -F'= ?' '/length/||/num/{print $2}' data
like image 197
user000001 Avatar answered Oct 08 '22 03:10

user000001


You can also pass multiple patterns to grep with the -e option

grep -e num -e length
like image 39
glenn jackman Avatar answered Oct 08 '22 05:10

glenn jackman


This should do the trick:

$ grep '^\w*=' file
num=12
length= 128
num= 24 
length= 128

Explanation:

^   # Start of line
\w  # Word class, shorthand for [a-zA-Z0-9_]
*   # Quantifier (zero or more)
=   # Equals character

The + quantifier is probably better (one or more) which is part of ERE (Extended Regular Expressions) so you'd need to used egrep (grep -E). This means lines that start with an = and no variable name will not be matched.

$ egrep '^\w+=' file
num=12
length= 128
num= 24 
length= 128 

Edit:

For you secondary question found in the comments, to only print the digit value we get into fancy uses of grep:

$ grep -Po '^\w+=\s?\K\d+' file
12
128
24
128

Or use a scripting language like awk

$ awk -F'= ?' '/\w*=/{print $2}' file
12
128
24 
128
like image 21
Chris Seymour Avatar answered Oct 08 '22 05:10

Chris Seymour