Text Processing: Grep - compare two files

From WikiMLT
Revision as of 11:12, 4 August 2022 by Spas (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Source of the ar­ti­cle: Ask Ubun­tu: Com­par­ing con­tents of two files.

Get on­ly the lines that ex­ist in file1 but not in file2:

grep -Fxvf file2 file1 > diff_file

Where:

  • -F, –fixed-strings – PAT­TERNS are strings,
  • -x, –line-reg­exp – match on­ly whole lines,
  • -v -
  • -f -

An in­line script for two ways com­par­i­son:

FILE1="file1"; FILE2="file2"; \
cat <(echo -e "\nOnly in $FILE1") \
    <(grep -Fvxf "$FILE2" "$FILE1") \
    <(echo -e "\nOnly in $FILE2") \
    <(grep -Fvxf "$FILE1" "$FILE2")