0

Hello I have 2 files with these words:

file: old.txt

toto
tata
tutu

file: new.txt

toto
tata
titi
tete

I would like to know the elements in more and less of these 2 lists.

in addition: titi, tete

less: tutu

3 Answers 3

2

The comm command does that

~/Temp$ comm -23  <(sort old.txt) <(sort new.txt)
tutu

~/Temp$ comm -13  <(sort old.txt) <(sort new.txt)
tete
titi

You can also print both results:

~/Temp$ comm -3  <(sort old.txt) <(sort new.txt)
      tete
      titi
tutu

Where option meaning is described in the comm man page :

  • -1 suppress lines unique to FILE1
  • -2 suppress lines unique to FILE2
  • -3 suppress lines that appear in both files
1

Using grep:

$ grep -v -w -f old new
titi
tete
$ grep -v -w -f new old 
tutu
4
  • This method gets me false results comparing with the comm mehtod. May 19, 2022 at 15:30
  • @ThibaudHulin You are not getting titi, tete and tutu? What are you getting instead? May 19, 2022 at 15:40
  • Here is an example where it doens't get what I want : justpaste.it/8lcpk Jun 11, 2022 at 11:42
  • @ThibaudHulin Oh, I didn't ignore partial matches in the original answer as there were none (grep switch -w, Select only those lines containing matches that form whole words.). Added the switch to above solution. Thanks for the heads up, sir. Jun 12, 2022 at 13:07
0
diff old.txt new.txt | sed -n 's/^</less:/p;s/^>/in addition:/p'

Use diff to show the differences and then use sed to reformat with "less:" and "in addition:"

Output:

less: tutu
in addition: titi
in addition: tete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.