bash 正则表达式中的AWK行尾符号

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

AWK end of line sign in regular expressions

regexbashsedawk

提问by Mikhail Kalashnikov

I have a simple awk script named "script.awk" that contains:

我有一个名为“script.awk”的简单 awk 脚本,其中包含:

/\/some_simple_string/ { print 
14 catcat one_two/some_thing
15 catcat one_three/one_more_some_simple_string
16 dogdog one_two/some_simple_string_again
17 dogdog one_four/some_simple_string
18 qweqwe firefire/ppp
;}

I'm using it to parse some file that contains: (by using: cat file | awk -f script.awk)

我正在使用它来解析一些包含以下内容的文件:(通过使用:cat file | awk -f script.awk)

17 dogdog one_four/some_simple_string

I want the script to only print the stroke that fully reflect "/some_simple_string[END_OF_LINE]" but not 2 or 3. Is there any simple way to do it?

我希望脚本只打印完全反映“/some_simple_string[END_OF_LINE]”的笔画,而不是 2 或 3。有什么简单的方法可以做到?

I think, the most appropriate way is to add end-of-line sigh to the regular expression. So it will parse only strokes that starting with "/some.." and have a new line at the end of "..string[END_OF_LINE]"

我认为,最合适的方法是在正则表达式中添加行尾叹气。所以它只会解析以“/some..”开头的笔画,并在“..string[END_OF_LINE]”的末尾有一个新行

Desired output:

期望的输出:

/\/some_simple_string$/ { print 
/\/some_simple_string$/ { print 
/some_simple_string/ { x = 
$ grep '/some_simple_string$' file
} END{ print x }
}
;}

Sorry for confusion, I was asking for END OF LINE sign in regular expressions. The correct answer is:

抱歉造成混淆,我要求在正则表达式中使用 END OF LINE 符号。正确答案是:

$ awk '/[/]some_simple_string$/' file

回答by Alderath

You can always use:

您可以随时使用:

$ awk 'print_flag{print;f=0} /[/]some_simple_string$/{print_flag=1}' file

I.e. match not only "some_simple_string" but match "/some_simple_string" followed by the end of the line ($ is end of line in regex)

即不仅匹配“some_simple_string”而且匹配“/some_simple_string”后跟行尾($是正则表达式中的行尾)

回答by lc2817

grep '\some_simple_string$' file | tail -n 1should do the trick.

grep '\some_simple_string$' file | tail -n 1应该做的伎俩。

Or if you really want to use awkdo awk '/\/some_simple_string/{x = $0}END{print x}'

或者如果你真的想使用awkdoawk '/\/some_simple_string/{x = $0}END{print x}'

回答by luser droog

To return just the lastof a group of matches, ...

要仅返回一组匹配项中的最后一个,...

Store the line in a variable and print it in the ENDblock.

将该行存储在变量中并将其打印在END块中。

$ grep -A1 '/some_simple_string$' file | tail -n 1

回答by Chris Seymour

To print all the matches that end with the string /some_simple_stringusing regular expression you need to anchor to the the end of the line using $. The most suitable tool for this job is grep:

/some_simple_string使用正则表达式打印以字符串结尾的所有匹配项,您需要使用$. 最适合这项工作的工具是grep

##代码##

In awkthe command is much the same:

awk命令中大致相同:

##代码##

To print all lines after the matching you would do:

要在匹配后打印所有行,您可以执行以下操作:

##代码##

Or just combine grepand tailif it makes it clearer using context option -Ato print the following lines:

或者只是组合greptail如果使用上下文选项-A打印以下行使其更清晰:

##代码##