Of course you can just use a simple diff to output the difference between files. But how about if one day you need to generate a custom report from that diff, for example, what is the line number difference between the files? Well, you may need some diff commands like below to save your day.
Don't be scared by this command. Once you break it down and it is quite easy to understand.
diff file1.txt file2.txt --changed-group-format='line %dF to line %dL is different|' --unchanged-line-format='' | sed 's/|/\n/g'
The output will be something like
=================================
line 2 to line 1 is different
line 7 to line 6 is different
A brief introduction to sed
sed is used for manupulating strings, for example, you can remove lines, append lines, subsitute string ... etc. You can also use regular expression to filter your strings ( well, actually regular expression is the most used case for sed ).
Here is a simple sed command
cat mood.txt | sed -e 's/happy/happier/' > mood.txt
This command read the output from mood.txt, replace all happy to happier, and then replace what is inside the mood.txt.
The parameter -e means using the following criteria / command.
cat mood.txt | sed -f myCommand.txt > mood.txt
The parameter -f means using the commands in the file as the criteria. So for example, if you have bunch of commands, you can write that into a file.
Well, sed is much more powerful than just the above, keep exploring you will find more ~
Don't be scared by this command. Once you break it down and it is quite easy to understand.
diff file1.txt file2.txt --changed-group-format='line %dF to line %dL is different|' --unchanged-line-format='' | sed 's/|/\n/g'
- --changed-group-format ~ what to output if the line is different
- --unchanged-line-format ~ what to output if the line is the same
- - sed is a text manuplulate program.
Note : Thanks nano for the comment
The output will be something like
=================================
line 2 to line 1 is different
line 7 to line 6 is different
A brief introduction to sed
sed is used for manupulating strings, for example, you can remove lines, append lines, subsitute string ... etc. You can also use regular expression to filter your strings ( well, actually regular expression is the most used case for sed ).
Here is a simple sed command
cat mood.txt | sed -e 's/happy/happier/' > mood.txt
This command read the output from mood.txt, replace all happy to happier, and then replace what is inside the mood.txt.
The parameter -e means using the following criteria / command.
cat mood.txt | sed -f myCommand.txt > mood.txt
The parameter -f means using the commands in the file as the criteria. So for example, if you have bunch of commands, you can write that into a file.
Well, sed is much more powerful than just the above, keep exploring you will find more ~
you lack a 'g' at the end of the s command, otherwise it will only replace first line.
ReplyDeletediff file1.txt file2.txt --changed-group-format='line %dF to line %dL is different|' --unchanged-line-format='' | sed 's/|/\n/g'