Parsing Result of Diff in Shell Script

Parsing result of Diff in Shell Script

Do you care about what the actual differences are, or just whether the files are different? If it's the latter you don't need to parse the output; you can check the exit code instead.

if diff -q "$source_file" "$dest_file" > /dev/null; then
: # files are the same
else
: # files are different
fi

Or use cmp which is more efficient:

if cmp -s "$source_file" "$dest_file"; then
: # files are the same
else
: # files are different
fi

Git diff parse in shell script

Pipe the output into

grep '^[+-][0-9]'

So:

diff --git a/lids b/lids | grep '^[+-][0-9]'

How to output 'passed' if a diff command results in no difference in bash?

Get and check the exit code from the diff command. diff has an exit code of 0 if no differences were found.

diff ...
ret=$?

if [[ $ret -eq 0 ]]; then
echo "passed."
else
echo "failed."
fi

How to parse svn diff results?

Like this with sed:

$ svn diff --summarize | sed -e '/^D/d' -e 's/.*host//'
/example/test3.php
/example/test4.php
/example/test5.php
/example/test6.php
/example/test7.php
/example/test8.php
/example/test9.php
/example/test10.php
/example/test11.php
/example/test12.php
/example/test13.php
/example/test.php
/test0.php

# Redirect output to file
$ svn diff --summarize | sed -e '/^D/d' -e 's/.*host//' > file

You need to pipe | the output of svn to sed. The first part '/^D/d' deletes all the lines starting with D and the second s/.*host// substitutes everything up to host with nothing, to store in to a file use redirect > file.

Similar logic with grep:

$ svn diff --summarize | grep '^[^D]' file | grep -Po '(?<=host).*' > file

The first grep filters out the lines that start with D and the second uses positive lookahead with -Po to display only the part of the line after host.



Related Topics



Leave a reply



Submit