bash Grep 做一件事,然后从下面的行中剪下

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11582026/
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-18 02:49:39  来源:igfitidea点击:

Grep for one thing then cut from the line below

linuxbashshellscriptinggrep

提问by bloodstorm17

Okay so I can't figure this out. Like a file that I using grep in order to get certain information has it set-up like this.

好吧,所以我无法弄清楚这一点。就像我使用 grep 为了获取某些信息而设置的文件一样。

#aaaa
<numbers 123456>

blah
blah
blah

#bbbb
<numbers 2156488>

blah
blah
blah

#cccc
<numbers 5478624>

blah
blah
blah

What I am doing is I am greping for aaaaor bbbbor ccccand the information that i really need is the numbers. As in when i grep for aaaathe thing i want to obtain is really just the numbers right below it. In this case it would 123456

我正在做的是我正在寻找aaaabbbbcccc我真正需要的信息是数字。就像当我搜索aaaa我想要获得的东西时一样,它实际上只是它下面的数字。在这种情况下,它会123456

I know how to grep for aaaabut i don't know how to go to the next line and cut the number.

我知道如何 grep foraaaa但我不知道如何转到下一行并减少数字。

回答by perreal

Using sed:

使用 sed:

sed -n '/aaaa/{
N
s/[^0-9]*\([0-9]*\).*//p
}' input_file

回答by Mihai Maruseac

You can use the -Coption of grepto show one line of context. Then you can tailon the last line (-n 1), cutusing spaces and selecting the second field, cutagain using >and selecting the first field. Thus:

您可以使用-C选项grep来显示一行上下文。然后您可以tail在最后一行 ( -n 1) 上cut使用空格并选择第二个字段,cut再次使用>并选择第一个字段。因此:

$ grep aaaa file -C 1 | tail -n 1 | cut -f2 -d' ' | cut -d'>' -f1
123456

will give back the number you're requesting.

将返回您请求的号码。

The most important part is the -Coption of grep

最重要的部分是-C选项grep

回答by Jonathan Leffler

It looks more like a job for sedto me:

sed对我来说,它看起来更像是一份工作:

sed -n '/^#[a-z]\{4\}/{ N; s/#.*\n<numbers //; s/>//p; }'

The -nsays don't print by default. The /^#[a-z]\{4\}/looks for lines like #aaaa; the actions inside { ... }apply only to such lines. The Nmeans 'read the next line'; the first s///removes the material before the number; the second removes the trailing >and prints.

-n说默认情况下不打印。在/^#[a-z]\{4\}/看起来像线#aaaa; 里面的动作{ ... }只适用于这些行。的N装置“读取下一行”; 第一个s///去除数字之前的材料;第二个删除尾随>和打印。

There are other options on how to do the 'delete irrelevant material', such as just one substitute command: s/[^0-9]*\([0-9][0-9]+\).*/\1/; this captures the first string of digits and removes everything else. It will handle more variations in the input than the more constrained regular expressions originally shown.

关于如何执行“删除无关材料”还有其他选项,例如只有一个替代命令:s/[^0-9]*\([0-9][0-9]+\).*/\1/; 这将捕获第一串数字并删除其他所有内容。与最初显示的更受约束的正则表达式相比,它将处理更多的输入变化。

Output from sample data:

样本数据的输出:

123456
2156488
5478624

This isn't automatically a job for sed; change it so that the interesting information was the third line after the match and it would be getting fiddly in sed(though N;N;N;probably does what's wanted).

这不是自动的工作sed;更改它,以便有趣的信息出现在比赛后的第三行,并且它会变得繁琐sed(尽管N;N;N;可能会做想要的事情)。

回答by bloodstorm17

@ Lars Kotthoff

@拉尔斯·科特霍夫

Your suggestion to use the -A 1option worked perfectly!

您使用该-A 1选项的建议非常有效!

The answer using your input is this:

使用您的输入的答案是这样的:

grep "aaaa" file -A 1 | grep "<numbers" | cut -d" " -f2 | cut -d">" -f1

I would love to give you the credit for this one!

我很乐意给你这个荣誉!

回答by slitvinov

If you are not insisting on grep
+0to get rid of ">"

如果你不是坚持grep
+0要去掉“>”

awk 'f{print +0; exit} /^#aaaa/{f=1}' foo.txt

Or

或者

awk 'f{print +0; f=0} /^#aaaa/{f=1}' foo.txt

回答by askmish

Try this one:

试试这个:

for i in "aaaa bbbb"
do
sed -n '/'"$i"'/,+1 p' test |tail -n1|cut -d' ' -f2| sed 's/.$//'
done

Its not very efficient but does the job fine.

它的效率不是很高,但可以很好地完成工作。