Linux – How to grep multiple lines from a file in linux

administratorbashbash-scriptinglinuxUbuntu

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,

Best Answer

Using grep:

grep -E 'hello|world' file

Using awk:

awk '/hello|world/' file
Related Question