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?
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.
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!
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.
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.
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
You can also pass multiple patterns to grep with the -e
option
grep -e num -e length
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With