How to grep multiple lines from a file in linux
6,814
Solution 1
Using grep
:
grep -E 'hello|world' file
Using awk
:
awk '/hello|world/' file
Solution 2
You can use grep
with the -x
option to match whole line and (Extended) Regex operator, |
, to perform a logical OR between patterns:
grep -xE 'hello|world' file.txt
If your grep
doesn't support the -E
option, use Basic Regex with escaped |
:
grep -x 'hello\|world' file.txt
Additionally, if you can't use the -x
option to match the whole line, use Regex operators:
grep -E '^(hello|world)$' file.txt
Related videos on Youtube
Author by
Uvais Ibrahim
Enthusiastic in Linux/Unix. Trying to learn the soul of Linux. :)
Updated on September 18, 2022Comments
-
Uvais Ibrahim over 1 year
I have a file with the below content.
. . hello . . . world . . hello . . . . . world . .
the dots indicates the other lines in the file. Here what I need to gerp only the lines hello and world. That means the output should something like below.
hello world hello world
How to accomplish this ?
Thanks,