How to search for a line which contains specific string in Bash?

1

I've got a file which contains some amount of lines, I want to look for a specific string, and if the file contains it, then I want to print a different line from the same file. for example, let's assume I have the file:

aaa
bbb
ccc

Let's say that I'm looking for bbb, and if I find it I want to print the following line (in the example, ccc).

I know that with grep I can easily search for the "bbb", but what command can I use in order to print a different line of the same file?

Danny

Posted 2015-04-14T10:51:49.540

Reputation: 13

Answers

3

There are these options in grep:

-A NUM will give you NUM lines after.

-B NUM* will give you NUM lines before.

-C NUM both (NUM lines before and NUM lines after).

Where NUM is an integer number.

In your case grep -A 1 bbb file.txt will give you ccc.

jcbermu

Posted 2015-04-14T10:51:49.540

Reputation: 15 868