Text Processing: Grep - compare two files: Difference between revisions
From WikiMLT
m Стадий: 5 [Фаза:Утвърждаване, Статус:Авторизиран]; Категория:Linux Server |
mNo edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
<noinclude><!--[[Category:Linux_Server|?]]-->{{ContentArticleHeader/Linux_Server}}</noinclude> | <noinclude><!--[[Category:Linux_Server|?]]-->{{ContentArticleHeader/Linux_Server}}</noinclude> | ||
'''''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].''''' | ||
Line 29: | Line 22: | ||
{{devStage | {{devStage | ||
| Прндл = Linux Server | | Прндл = Linux Server | ||
| Стадий = | | Стадий = 6 | ||
| Фаза = Утвърждаване | | Фаза = Утвърждаване | ||
| Статус = | | Статус = Утвърден | ||
| ИдтПт = Spas | | ИдтПт = Spas | ||
| РзбПт = Spas | | РзбПт = Spas |
Latest revision as of 10:17, 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")
From the comments:
- The problem with this solution is that it'll go super slow if you've got long files (it's O(N^2) on the length of the longer file). Sorting first and using something like
diff
orcomm
will be O(N log N).