grep + A:匹配后打印所有内容 [英] grep +A: print everything after match

查看:688
本文介绍了grep + A:匹配后打印所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个文件包含一个网址列表,如下所示:

file1:

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

我想在以后获取所有记录: http://www.yahoo.com ,结果如下所示:



file2:

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

我知道我可以使用grep来查找yahoo.com所在的行号

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

但是我不知道如何在第3行之后得到这个文件。另外,我知道在grep -A中有一个标记会在匹配后打印出来。但是,您需要指定匹配后您想要的行数。我想知道有什么可以解决这个问题。像:

 伪代码:
$ grep -n'http://www.yahoo.com'-A所有file1> file2

我知道我们可以使用我得到的行号和wc -l来获得行数在yahoo.com之后,然而..感觉非常跛脚。

期待一个方便和简单的解决方案。
自由批评我在一开始就对问题进行复杂化处理,并欢迎awk和sed命令! 如果您不介意使用awk:

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


  

/ yahoo / {y = 1;第一部分指出,如果我们遇到了一条与 yahoo相关的行,那么我们可以使用 yahoo ,我们设置变量y = 1,然后跳过该行( next >)将跳转到下一行,从而跳过当前行的任何进一步处理)。如果没有下一步命令,则会打印 yahoo 行。 第二部分对于:

  y!= 0 {print} 

pre>

这意味着,对于每一行,如果变量y不为零,我们会打印该行。在awk中,如果引用一个变量,那么将根据上下文创建该变量并且为零或空字符串。在遇到 yahoo 之前,变量y是0,所以脚本不会打印任何东西。在遇到 yahoo 后,y是1,所以之后的每一行都会被打印出来。

sed



或者,使用 sed ,以下内容将删除所有与yahoo相关的内容:

  sed'1,/ yahoo / d'data.txt 


Hi I have a file contains a list of urls, 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:

PSEUDO CODE:
$ 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.. feels pretty lame.

Looking forward to a handy and easy solution. Feel free criticizing me for complexifying the problem right at the beginning and awk and sed commands are also welcome!

解决方案

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, 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 

这篇关于grep + A:匹配后打印所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆