Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'grep +A': print everything after a match [duplicate]

Tags:

grep

bash

sed

awk

I have a file that contains a list of URLs. It looks like below:

file1:

http://www.google.com http://www.bing.com http://www.yahoo.com http://www.baidu.com http://www.yandex.com .... 

I want to get all the records after: http://www.yahoo.com, results looks like below:

file2:

http://www.baidu.com http://www.yandex.com .... 

I know that I could use grep to find the line number of where yahoo.com lies using

grep -n 'http://www.yahoo.com' file1  3 http://www.yahoo.com 

But I don't know how to get the file after line number 3. Also, I know there is a flag in grep -A print the lines after your match. However, you need to specify how many lines you want after the match. I am wondering is there something to get around that issue. Like:

Pseudocode:  grep -n 'http://www.yahoo.com' -A all file1 > file2 

I know we could use the line number I got and wc -l to get the number of lines after yahoo.com, however... it feels pretty lame.

like image 298
B.Mr.W. Avatar asked Aug 10 '13 21:08

B.Mr.W.


People also ask

How do you grep the nth line after a match?

To get the n-th line after each match, we can first use grep -An to find each block with n+1 lines. Next, instead of piping it to grep -v, we pipe it to a command that can print every (n+1)-th line.

How do I grep and print next line?

You can use grep with -A n option to print N lines after matching lines. Using -B n option you can print N lines before matching lines. Using -C n option you can print N lines before and after matching lines.


1 Answers

AWK

If you don't mind using AWK:

awk '/yahoo/{y=1;next}y' data.txt 

This script has two parts:

/yahoo/ { y = 1; next } y 

The first part states that if we encounter a line with yahoo, we set the variable y=1, and then skip that line (the next command will jump to the next line, thus skip any further processing on the current line). Without the next command, the line yahoo will be printed.

The second part is a short hand for:

y != 0 { print } 

Which means, for each line, if variable y is non-zero, we print that line. In AWK, if you refer to a variable, that variable will be created and is either zero or empty string, depending on context. Before encounter yahoo, variable y is 0, so the script does not print anything. After encounter yahoo, y is 1, so every line after that will be printed.

Sed

Or, using sed, the following will delete everything up to and including the line with yahoo:

sed '1,/yahoo/d' data.txt 
like image 190
Hai Vu Avatar answered Oct 03 '22 10:10

Hai Vu