Using sed to extract result from diffstat

90 Views Asked by At

I am trying to use sed to get the number of insertions/deletions from diff stat. For example, diffstat gives something like "1 file changed, 2 insertions(+), 1 deletion(-)". How can I retrieve "2" from diffstat using sed? I can't seem to figure it out.

Thanks for your time.

Figured out very simple solution-

sed 's|.*\s\(.*\)\sinsertion.*|\1|'
2

There are 2 best solutions below

0
Jared Casper On
sed 's/.*, \(.*\) insertions.*/\1/'

Explanation: look for "[anything], [something] insertions[anything]" and replace it with [something]. Might want to pass the diffstat through grep first to isolate this one line.

0
Mark Setchell On

Try this:

echo "1 file changed, 2 insertions(+), 1 deletion(-)" | sed -r 's/.*([0-9]+) insertion.*/\1/'