Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cut part of grep output with sed

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

i need 4 commands that result was like

first command (tests run)

1042

second command (Failures)

0

third command (Errors)

0

fourth commad (Skipped)

0
like image 703
Roman Iuvshin Avatar asked Dec 22 '22 06:12

Roman Iuvshin


2 Answers

You can use the following four commands to obtain the values for Tests run, Failures, Errors, and Skipped respectively:

cat log.log | sed 's/Tests run: \([0-9]\+\).*/\1/g'
cat log.log | sed 's/.*Failures: \([0-9]\+\).*/\1/g'
cat log.log | sed 's/.*Errors: \([0-9]\+\).*/\1/g'
cat log.log | sed 's/.*Skipped: \([0-9]\+\).*/\1/g'
like image 69
Mansoor Siddiqui Avatar answered Dec 23 '22 19:12

Mansoor Siddiqui


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
like image 25
glenn jackman Avatar answered Dec 23 '22 20:12

glenn jackman