bash 如何在比赛前打印所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11270381/
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
How can I print everything before match?
提问by user1492268
I am trying to use awk match() and substr to print everything before the match occurs. I am using awk in a bash script to pull out a string from a file name, and have tried the following:
我正在尝试使用 awk match() 和 substr 在匹配发生之前打印所有内容。我在 bash 脚本中使用 awk 从文件名中提取字符串,并尝试了以下操作:
awk 'match(awk 'match(sed 's/\(.*\.S..\).*//'
, "\.S") {print substr(sed 's/\.S../&\n/; s/\n.*//'
, 1, RSTART + 3)}'
,".S") {print substr(perl -nle 'print "$`$&" if /\.S../'
, RSTART+1, 3)}'
This gives me 3 chars, Splus the 2 following it.
这给了我 3 个字符,S加上它后面的 2个字符。
But, I want is to print everything before the match.
但是,我想要的是在比赛前打印所有内容。
How can I do that?
我怎样才能做到这一点?
回答by Paused until further notice.
Just a minor rearrangement:
只是一个小的重新排列:
awk '{i = index(sed 's/.S.*//'
, ".S"); if (i>0) print substr(##代码##, 0, i+5)}'
This tells AWK to print starting from the first character through the "S" and the next two characters.
这告诉 AWK 从第一个字符开始打印,通过“S”和接下来的两个字符。
To get the same result from sed:
要从以下获得相同的结果sed:
Simple version (".S" can only appear once:
简单版本(“.S”只能出现一次:
##代码##If ".S" can appear more than once, this version gives the same result as the AWK version (only the first ".S" and its two following characters are kept):
如果“.S”可以出现多次,则此版本给出与 AWK 版本相同的结果(仅保留第一个“.S”及其后两个字符):
##代码##Edit:
编辑:
##代码##回答by bsravanin
This should work for you.
这应该对你有用。
##代码##回答by Thor
I would rather use:
我宁愿使用:
##代码##
