0

File1

james:hello
richard:hello2
johnson:hello3
jam3es:hello
ja1mes:hello

File2

james:hello
ja1mes:hello
johnson:hello3

Expected output

richard:hello2
jam3es:hello

I have these 2 files, I'm trying to compare first column aka the name(s) with the other file name(s) and print out unique line(s) from File1.

4
  • @Quasímodo I would like to use join for this, both files are sorted by the first column
    – Johnson
    May 20 at 19:29
  • How would you get that output? What's the pattern? May 20 at 19:41
  • 2
    To be clear, are you trying to get unique lines from the files, or only unique names, so if the part after the colon was different, should it show up?
    – Ember
    May 20 at 20:19
  • What exactly is your question? May 20 at 22:19
2

The files don't appear to be sorted on the first column in any locale that I can imagine, but if you do sort them then you can trivially use join with the -v option ex.

$ join -t ':' -v 1 <(sort -t ':' file1) <(sort -t ':' file2)
jam3es:hello
richard:hello2

If you really need output in the order given, then you can pipe the results to another sort command.

1

awk gives a nice concise solution

awk -F: '
    NR == FNR {name[$1]; next}
    !($1 in name)
' file2 file1

outputs

richard:hello2
jam3es:hello

The sample input files show that the whole lines are duplicated. If that's actually the case, the comm utility is handy:

comm -23 <(sort file1) <(sort file2)

outputs the lines that only appear in file 1 (option -2 suppresses lines that only appear in file 2, and -3 suppresses common lines)

jam3es:hello
richard:hello2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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