Linux 用 sed 剪切 grep 输出的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8096896/
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
cut part of grep output with sed
提问by Roman Iuvshin
I need to get values from log file, values may be different and need to get it.
我需要从日志文件中获取值,值可能不同,需要获取它。
example of string:
字符串示例:
Tests run: 1042, Failures: 0, Errors: 0, Skipped: 0
i try with:
我尝试:
cat log.log | grep '^Test run:' | sed -e 's/^Test run: //'
but i get: 1042, Failures: 0, Errors: 0, Skipped: 0
但我得到:1042,失败:0,错误:0,跳过:0
i need 4 commands that result was like
我需要 4 个命令,结果就像
first command (tests run)
第一个命令(测试运行)
1042
second command (Failures)
第二个命令(失败)
0
third command (Errors)
第三个命令(错误)
0
fourth commad (Skipped)
第四个命令(跳过)
0
采纳答案by Mansoor Siddiqui
You can use the following four commands to obtain the values for Tests run
, Failures
, Errors
, and Skipped
respectively:
您可以使用以下四种命令来获得的值Tests run
,Failures
,Errors
,和Skipped
分别为:
cat log.log | sed 's/Tests run: \([0-9]\+\).*//g'
cat log.log | sed 's/.*Failures: \([0-9]\+\).*//g'
cat log.log | sed 's/.*Errors: \([0-9]\+\).*//g'
cat log.log | sed 's/.*Skipped: \([0-9]\+\).*//g'
回答by bmk
Do you mean something like this?
你的意思是这样的吗?
sed -rn 's/^Tests run:\s*//;s/,[^:]+:\s*/\n/gp' log.log
Result:
结果:
1042
0
0
0
回答by jintoreedwine
I'm not sure if this will be what you want or not, but assuming the lines you are searching for will be in this format:
我不确定这是否是您想要的,但假设您要搜索的行将采用以下格式:
Tests run: <integer>, Failures: <integer>, Errors: <integer>, Skipped: <integer>
The following might work for you:
以下可能对您有用:
sed -e 's/^Tests run: \([0-9]*\), Failures: \([0-9]*\), Errors: \([0-9]*\), Skipped: \([0-9]\).*$/,,,/'
Given the following input in a file called test.log
给定名为 test.log 的文件中的以下输入
Tests run: 1042, Failures: 0, Errors: 0, Skipped: 0
Tests run: 12, Failures: 0, Errors: 0, Skipped: 0
Tests run: 104, Failures: 1, Errors: 3, Skipped: 4
Running:
跑步:
cat test.log | sed -e 's/^Tests run: \([0-9]*\), Failures: \([0-9]*\), Errors: \([0-9]*\), Skipped: \([0-9]\).*$/,,,/'
Would yield the following output:
将产生以下输出:
1042,0,0,0
12,0,0,0
104,1,3,4
You could, of course, tweak the final output by playing with the /\1,\2,\3,\4/ part of the sed expression.
当然,您可以通过使用 sed 表达式的 /\1,\2,\3,\4/ 部分来调整最终输出。
回答by ks1322
Something like this:
像这样的东西:
sed -n "s/.*Tests run: \([0-9]*\),.*//p"
sed -n "s/.*Failures: \([0-9]*\),.*//p"
sed -n "s/.*Errors: \([0-9]*\),.*//p"
sed -n "s/.*Skipped: \([0-9]*\)$//p"
回答by glenn Hymanman
grep '^Tests run:' <<END | grep -o '[0-9]\+'
Tests run: 1042, Failures: 0, Errors: 0, Skipped: 0
junk
Tests run: 1, Failures: 2, Errors: 3, Skipped: 4
END
outputs
产出
1042
0
0
0
1
2
3
4
回答by potong
This might work for you:
这可能对你有用:
echo "Tests run: 1042, Failures: 0, Errors: 0, Skipped: 0" |
sed '/^Tests/{s/[^0-9,]//g;y/,/\n/;}'
1042
0
0
0