如何获取文件的匹配grep的前pression行后的部分? (第一场比赛) [英] How to get the part of file after the line that matches grep expression ? (first match)

查看:244
本文介绍了如何获取文件的匹配grep的前pression行后的部分? (第一场比赛)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约1000行的文件。我想它匹配的grep我的发言行后我的文件的一部分。

I have a file with about 1000 lines. I want the part of my file after the line which matches my grep statement.

$ cat file | grep 'TERMINATE'     // Its found on line 534

所以,我想从行的文件 535线1000 作进一步处理。

我该怎么办呢?

推荐答案

下面将打印线路匹配将中止,直到文件的末尾:

The following will print the line matching TERMINATE till the end of the file:

sed -n -e '/TERMINATE/,$p'

说明: -n 禁用打印每行后的 SED 的默认行为执行其脚本就可以了, -e 表示脚本 SED /取消/, $ 是一个地址(线)范围选择意味着第一行匹配将中止常规EX pression(如grep)在2002年底该文件( $ )和 p 是打印当前行的打印命令。

Explained: -n disables default behavior of sed of printing each line after executing its script on it, -e indicated a script to sed, /TERMINATE/,$ is an address (line) range selection meaning the first line matching the TERMINATE regular expression (like grep) to the end of the file ($), and p is the print command which prints the current line.

这将打印从下面的线路匹配行将中止,直到文件的末尾:结果
(从匹配行之后EOF,不包括配套系)

This will print from the line that follows the line matching TERMINATE till the end of the file:
(from AFTER the matching line to EOF, NOT including the matching line)

sed -e '1,/TERMINATE/d'

说明: 1,/取消/ 是一个地址(线)范围选择意为输入的第一行第一线匹配在将中止常规EX pression和 D 是删除命令,它删除当前行并跳到下一行。由于 SED 默认行为是打印的线条,将打印的行之后将中止来输入的结束。

Explained: 1,/TERMINATE/ is an address (line) range selection meaning the first line for the input to the 1st line matching the TERMINATE regular expression, and d is the delete command which delete the current line and skip to the next line. As sed default behavior is to print the lines, it will print the lines after TERMINATE to the end of input.

编辑:

如果你想行之前将中止

sed -e '/TERMINATE/,$d'

如果你想两条线之前和之后将中止在单次2个不同的文件:

And if you want both lines before and after TERMINATE in 2 different files in a single pass:

sed -e '1,/TERMINATE/w before
/TERMINATE/,$w after' file

在之前和之后的文件将包含终止该行,所以处理每一个你需要使用:

The before and after files will contain the line with terminate, so to process each you need to use:

head -n -1 before
tail -n +2 after

EDIT2:

如果你不想硬code中的文件名中的sed脚本,您可以:

IF you do not want to hard-code the filenames in the sed script, you can:

before=before.txt
after=after.txt
sed -e "1,/TERMINATE/w $before
/TERMINATE/,\$w $after" file

但你不得不逃离 $ 这意味着最后一行使外壳不会尝试展开 $ W 变量(注意,我们现在使用双引号周围的脚本,而不是单引号)。

But then you have to escape the $ meaning the last line so the shell will not try to expand the $w variable (note that we now use double quotes around the script instead of single quotes).

我忘了告诉大家,新的生产线是在脚本文件名之后重要的,这样的sed知道文件名末尾。

I forgot to tell that the new line is important after the filenames in the script so that sed knows that the filenames end.

结果
修改 2016-0530


2016-0530

塞巴斯蒂安克莱门特问道:你怎么会用一个变量替换硬codeD TERMINAL?

Sébastien Clément asked: "How would your replace the hardcoded TERMINAL by a variable?"

您会做出匹配的文本的变量,然后做的方式为previous例子一样的:

You would make a variable for the matching text and then do it the same way as the previous example:

matchtext=TERMINATE
before=before.txt
after=after.txt
sed -e "1,/$matchtext/w $before
/$matchtext/,\$w $after" file

要使用一个变量与previous例子匹配的文本:

to use a variable for the matching text with the previous examples:

## Print the line containing the matching text, till the end of the file:
## (from the matching line to EOF, including the matching line)
matchtext=TERMINATE
sed -n -e "/$matchtext/,\$p"

## Print from the line that follows the line containing the 
## matching text, till the end of the file:
## (from AFTER the matching line to EOF, NOT including the matching line)
matchtext=TERMINATE
sed -e "1,/$matchtext/d"

## Print all the lines before the line containing the matching text:
## (from line-1 to BEFORE the matching line, NOT including the matching line)
matchtext=TERMINATE
sed -e "/$matchtext/,\$d"

有关在这些情况下,变量替换文本的要点是:

The important points about replacing text with variables in these cases are:


  1. 包含在变量( $ VARIABLENAME )的单引号 [ ]不会扩大,但里面的双引号 []的意愿。所以,你必须改变所有的单引号双引号如果包含你想用一个变量来替换文本。

  2. SED 的范围也包含 $ 并紧跟着像一个字母: $ p $ d个 $ W 。他们也将像变量加以扩展,所以你必须逃生者 $ 字符用反斜杠[ \\ ]这样的: \\ $ p \\ $ d个 \\ $ W

  1. Variables ($variablename) enclosed in single quotes ['] won't "expand" but variables inside double quotes ["] will. So, you have to change all the single quotes to double quotes if they contain text you want to replace with a variable.
  2. The sed ranges also contain a $ and are immediately followed by a letter like: $p, $d, $w. They will also look like variables to be expanded, so you have to escape those $ characters with a backslash [\] like: \$p, \$d, \$w.

这篇关于如何获取文件的匹配grep的前pression行后的部分? (第一场比赛)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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