Text Processing: Grep - compare two files: Difference between revisions
From WikiMLT
Created page with "'''''Source of the article: [https://askubuntu.com/a/1030419/566421 Ask Ubuntu: Comparing contents of two files].''''' Get only the lines that exist in <code>file1</code> but not in <code>file2</code>:<syntaxhighlight lang="shell" line="1"> grep -Fxvf file2 file1 > diff_file </syntaxhighlight>Where: * <code>-F</code>, <code>--fixed-strings</code> - PATTERNS are strings, * <code>-x</code>, <code>--line-regexp</code> - match only whole lines, * <code>-v</code> - * <co..." |
mNo edit summary |
||
Line 1: | Line 1: | ||
'''''Source of the article: [https://askubuntu.com/a/1030419/566421 Ask Ubuntu: Comparing contents of two files].''''' | '''''Source of the article: [https://askubuntu.com/a/1030419/566421 Ask Ubuntu: Comparing contents of two files].''''' | ||
Get only the lines that exist in <code>file1</code> but not in <code>file2</code>:<syntaxhighlight lang="shell" line="1"> | Get only the lines that exist in <code class="noTypo">file1</code> but not in <code class="noTypo">file2</code>:<syntaxhighlight lang="shell" line="1"> | ||
grep -Fxvf file2 file1 > diff_file | grep -Fxvf file2 file1 > diff_file | ||
</syntaxhighlight>Where: | </syntaxhighlight>Where: | ||
* <code>-F</code>, <code>--fixed-strings</code> - PATTERNS are strings, | * <code class="noTypo">-F</code>, <code class="noTypo">--fixed-strings</code> - PATTERNS are strings, | ||
* <code>-x</code>, <code>--line-regexp</code> - match only whole lines, | * <code class="noTypo">-x</code>, <code class="noTypo">--line-regexp</code> - match only whole lines, | ||
* <code>-v</code> - | * <code class="noTypo">-v</code>, <code class="noTypo">--invert-match</code> - select non-matching lines, | ||
* <code>-f</code> - | * <code class="noTypo">-f</code>, <code class="noTypo">--file=FILE</code> - take PATTERNS from FILE. | ||
An inline script for two ways comparison:<syntaxhighlight lang="shell" line="1"> | An inline script for two ways comparison:<syntaxhighlight lang="shell" line="1"> |
Revision as of 10:15, 4 August 2022
Source of the article: Ask Ubuntu: Comparing contents of two files.
Get only the lines that exist in file1
but not in file2
:
grep -Fxvf file2 file1 > diff_file
Where:
-F
,--fixed-strings
– PATTERNS are strings,-x
,--line-regexp
– match only whole lines,-v
,--invert-match
– select non-matching lines,-f
,--file=FILE
– take PATTERNS from FILE.
An inline script for two ways comparison:
FILE1="file1"; FILE2="file2"; \
cat <(echo -e "\nOnly in $FILE1") \
<(grep -Fvxf "$FILE2" "$FILE1") \
<(echo -e "\nOnly in $FILE2") \
<(grep -Fvxf "$FILE1" "$FILE2")