bash grep 换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:23:25  来源:igfitidea点击:

bash grep newline

bashgrep

提问by Markus

[Editorial insertion: Possible duplicate of the same poster's earlier question?]

[编辑插入:可能与同一张海报的早期问题重复?]

Hi, I need to extract from the file:

您好,我需要从文件中提取:

first
second
third

using the grep command, the following line:

使用 grep 命令,以下行:

second
third

How should the grep command look like?

grep 命令应该是什么样子的?

回答by notnoop

Instead of grep, you can use pcregrepwhich supports multiline patterns

取而代之的是grep,您可以使用pcregrepwhich 支持多行模式

pcregrep -M 'second\nthird' file

-Mallows the pattern to match more than one line.

-M允许模式匹配多于一行。

回答by Andrew Y

Your question abstract "bash grep newline", implies that you would want to match on the second\nthirdsequence of characters - i.e. something containing newline within it.

您的问题摘要“bash grep newline”暗示您希望匹配second\nthird字符序列 - 即其中包含换行符的内容。

Since the grep works on "lines" and these two are different lines, you would not be able to match it this way.

由于 grep 在“行”上工作并且这两条是不同的行,因此您将无法以这种方式进行匹配。

So, I'd split it into several tasks:

所以,我会把它分成几个任务:

  1. you match the line that contains "second" and output the line that has matched and the subsequent line:

    grep -A 1 "second" testfile
    
  2. you translate every other newline into the sequence that is guaranteed not to occur in the input. I think the simplest way to do that would be using perl:

    perl -npe '$x=1-$x; s/\n/##UnUsedSequence##/ if $x;'
    
  3. you do a grep on these lines, this time searching for string ##UnUsedSequence##third:

    grep "##UnUsedSequence##third"
    
  4. you unwrap the unused sequences back into the newlines, sed might be the simplest:

    sed -e 's/##UnUsedSequence##/\n'
    
  1. 您匹配包含“second”的行并输出匹配的行和后续行:

    grep -A 1 "second" testfile
    
  2. 您将所有其他换行符转换为保证不会出现在输入中的序列。我认为最简单的方法是使用 perl:

    perl -npe '$x=1-$x; s/\n/##UnUsedSequence##/ if $x;'
    
  3. 你在这些行上做一个 grep ,这次搜索 string ##UnUsedSequence##third

    grep "##UnUsedSequence##third"
    
  4. 您将未使用的序列解包回换行符,sed 可能是最简单的:

    sed -e 's/##UnUsedSequence##/\n'
    

So the resulting pipe command to do what you want would look like:

因此,执行您想要的操作的结果管道命令如下所示:

grep -A 1 "second" testfile | perl -npe '$x=1-$x; s/\n/##UnUsedSequence##/ if $x;' | grep "##UnUsedSequence##third" | sed -e 's/##UnUsedSequence##/\n/'

Not the most elegant by far, but should work. I'm curious to know of better approaches, though - there should be some.

到目前为止不是最优雅的,但应该工作。不过,我很想知道更好的方法 - 应该有一些。

回答by Jim

I don't think grepis the way to go on this.

我不认为grep是解决这个问题的方法。

If you just want to strip the first line from any file (to generalize your question), I would use sedinstead.

如果您只想从任何文件中删除第一行(以概括您的问题),我会改用sed

sed '1d' INPUT_FILE_NAME

This will send the contents of the file to standard output with the first line deleted.

这会将文件的内容发送到标准输出并删除第一行。

Then you can redirect the standard output to another file to capture the results.

然后您可以将标准输出重定向到另一个文件以捕获结果。

sed '1d' INPUT_FILE_NAME > OUTPUT_FILE_NAME

That should do it.

应该这样做。

If you have to use grepand just don't want to display the line with firston it, then try this:

如果您必须使用grep并且不想显示带有first的行,请尝试以下操作:

grep -v first INPUT_FILE_NAME 

By passing the -vswitch, you are telling grepto show you everything butthe expression that you are passing. In effect show me everything but the line(s) with firstin them.

通过传递-v开关,你告诉grep的展现你的一切,但要传递的表达。实际上向我展示了除了带有first的那一行之外的所有东西。

However, the downside is that a file with multiple first's in it will not show those other lines either and may not be the behavior that you are expecting.

但是,缺点是其中包含多个first的文件也不会显示其他行,并且可能不是您期望的行为。

To shunt the results into a new file, try this:

要将结果分流到新文件中,请尝试以下操作:

grep -v first INPUT_FILE_NAME > OUTPUT_FILE_NAME

Hope this helps.

希望这可以帮助。

回答by liori

I don't really understand what do you want to match. I would not use grep, but one of the following:

我真的不明白你想匹配什么。我不会使用 grep,而是使用以下之一:

tail -2 file         # to get last two lines
head -n +2 file      # to get all but first line
sed -e '2,3p;d' file # to get lines from second to third

(not sure how standard it is, it works in GNU tools for sure)

(不确定它的标准如何,它肯定可以在 GNU 工具中使用)

回答by joshbooks

grep -A1 "second" | grep -B1 "third" works nicely, and if you have multiple matches it will even get rid of the original -- match delimiter

grep -A1 "秒" | grep -B1 "third" 效果很好,如果你有多个匹配项,它甚至会去掉原来的 -- 匹配分隔符

回答by Mark Rushakoff

So you just don't want the line containing "first"? -vinverts the grep results.

所以你只是不想要包含“first”的行? -v反转 grep 结果。

$ echo -e "first\nsecond\nthird\n" | grep -v first
second
third

回答by Rob Wells

Line? Or lines?

线?还是线路?

Try

尝试

grep -E -e '(second|third)' filename

Edit:grep is line oriented. you're going to have to use either Perl, sed or awk to perform the pattern match across lines.

编辑:grep 是面向行的。您将不得不使用 Perl、sed 或 awk 来执行跨行模式匹配。

BTW -E tell grep that the regexp is extended RE.

顺便说一句 -E 告诉 grep 正则表达式是扩展的 RE。

回答by Andrejs Cainikovs

grep -E '(second|third)' /path/to/file
egrep -w 'second|third' /path/to/file

回答by Evgeny

you could use

你可以用

$ grep -1 third filename

this will print a string with match and one string before and after. Since "third" is in the last string you get last two strings.

这将打印一个匹配的字符串和一个前后字符串。由于“第三个”在最后一个字符串中,因此您将获得最后两个字符串。

回答by hobs

I like notnoop's answer, but building on AndrewY's answer(which is better for those without pcregrep, but way too complicated), you can just do:

我喜欢notnoop 的回答,但基于AndrewY 的回答(这对于没有 pcregrep 的人来说更好,但太复杂了),你可以这样做:

RESULT=`grep -A1 -s -m1 '^\s*second\s*$' file | grep -s -B1 -m1 '^\s*third\s*$'`