Skip to main content

5. Comparing text

  1. Let's create two test files:

    cat > file1.txt <<EOF
    a
    b
    c
    d
    EOF
    cat > file2.txt <<EOF
    b
    c
    d
    e
    EOF
  2. We can compare them with comm:

    comm file1.txt file2.txt
    comm -12 file1.txt file2.txt

    In this case we are suppressing the columns 1 and 2.

  3. A more complex tool is diff:

    diff file1.txt file2.txt

    With context:

    diff -c file1.txt file2.txt

    Unified format is more concise:

    diff -u file1.txt file2.txt
  4. To create a patch file usually the options -Naur are used:

    diff -Naur file1.txt file2.txt > patchfile.txt
    cat patchfile.txt

    We can use the command patch to apply a patch file:

    patch file1.txt patchfile.txt
    cat file1.txt

    Now file1.txt has the same content as file2.txt.

Loading asciinema cast...