Linux 用grep解析字符串

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

Parsing string with grep

linuxparsingsedgrep

提问by Roman Iuvshin

I need some help with parsing a string in Linux.

我需要一些帮助来解析 Linux 中的字符串。

I have a string:

我有一个字符串:

[INFO] Total time: 2 minutes 8 seconds

and want to get only

并且只想得到

2 minutes 8 seconds

采纳答案by knittl

If the line prefix is always the same, simply use sed and replace the prefix with an empty string:

如果行前缀始终相同,只需使用 sed 并将前缀替换为空字符串:

sed 's/\[INFO\] Total Time: //'

Assuming that the time is always the last thing in a line after a colon, use the following regex (replace each line with everything after the colon):

假设时间总是冒号后一行中的最后一个,使用以下正则表达式(用冒号后的所有内容替换每一行):

sed 's/^.*: \(.*\)$//'

回答by yko

Use sedor perl:

使用sedperl

echo "[INFO] Total time: 2 minutes 8 seconds" | sed -e 's/^\[INFO\] Total time:\s*//'
echo "[INFO] Total time: 2 minutes 8 seconds" | perl -pe "s/^\[INFO\] Total time:\s*//;"

回答by sehe

The sed and perl options do work, but in this trivial case, I'd prefer

sed 和 perl 选项确实有效,但在这种微不足道的情况下,我更喜欢

 echo "[INFO] Total time: 2 minutes 8 seconds" | cut -d: -f2

If you have something against spaces, you can just use

如果你有反对空格的东西,你可以使用

 echo "[INFO] Total time: 2 minutes 8 seconds" | cut -d: -f2 | xargs

or even...

甚至...

 echo "[INFO] Total time: 2 minutes 8 seconds" | cut -d: -f2 | cut -c2-

PS. Trivia: you could do this with greponly if grep implemented positive lookbehind like this egrep -o '(?<=: ).*'; Unfortunately neither POSIX extended regex nor GNU extended regex implement lookbehind (http://www.regular-expressions.info/refflavors.html)

附注。琐事:grep只有当 grep 像这样实现了正向后视,你才能做到这一点egrep -o '(?<=: ).*';不幸的是,POSIX 扩展正则表达式和 GNU 扩展正则表达式都没有实现后视(http://www.regular-expressions.info/refflavors.html)

回答by Johnsyweb

Using grep:

使用grep

$ echo '[INFO] Total time: 2 minutes 8 seconds' | grep -o '[[:digit:]].*$'
2 minutes 8 seconds

Or sed:

或者sed

$ echo '[INFO] Total time: 2 minutes 8 seconds' | sed 's/.*: //'
2 minutes 8 seconds

Or awk:

或者awk

$ echo '[INFO] Total time: 2 minutes 8 seconds' | awk -F': ' '{print }'
2 minutes 8 seconds

Or cut:

或者cut

$ echo '[INFO] Total time: 2 minutes 8 seconds' | cut -d: -f2
 2 minutes 8 seconds

And then read sed & awk, Second Edition.

然后阅读sed & awk, Second Edition

回答by jaypal singh

If you prefer AWK then it is quite simple

如果你更喜欢 AWK 那么它很简单

echo "[INFO] Total time: 2 minutes 8 seconds" | awk -F": " '{ print  }'

回答by Shifu

If you are getting the info from the terminal then you can grep out the info and use cut with the delimiter to remove everything before the info you want. grep INFO | cut -f2 -d:

如果您从终端获取信息,那么您可以 grep 出信息并使用带有分隔符的 cut 删除您想要的信息之前的所有内容。grep 信息 | 剪切 -f2 -d:

If you want the info out of a file then you can grep the file grep INFO somefilename | cut -f2 -d:

如果你想从文件中获取信息,那么你可以 grep 文件 grep INFO somefilename | 剪切 -f2 -d: