I have 2 files File1.txt:

Column1    | Column2
username2  | timestamp
username1  | timestamp
username4  | timestamp


Column1    | Column2
username1  | timestamp
username3  | timestamp
username2  | timestamp

I want to output where they have matching Column1 values into a new file just showing Column1 content. Those values in Column1 are not always in the same order between file1.txt and file2.txt and some entries will be missing from either file.




Using awk

awk -F ' *| *' 'NR==FNR{a[$1];next}($1 in a)' file1 file2

The array a is filled with the content of the first file1 column. Only lines matching an entry array will be printed when next file is parsed.

  • Thanks Oliv, although that gave me everything; matches and differences. – Don Draper Jun 7 '18 at 15:02
  • One minor drawback: if userX appears in file1, and it appears twice in file2, you'll see both instances in the output. You might want: $1 in a {print $1; delete a[$1]} to only print the first one. – glenn jackman Jun 7 '18 at 16:05
  • In my case, there are no duplicates. – Don Draper Jun 7 '18 at 17:46
  • @Don, if this is the best answer, accept it. Read What should I do when someone answers my question? – glenn jackman Jun 7 '18 at 18:04

Extract column 1 from both files, sort it, then find the duplicated lines:

cut -d" " -f1 File1.txt File2.txt | sort | uniq -d
  • Thats almost it I reckon. Although there are single spaces infront of the first column text and those are being missed. How would you incorporate that? – Don Draper Jun 7 '18 at 15:38
  • Then cut is the wrong tool. Stick with awk. – glenn jackman Jun 7 '18 at 16:04
  • Could I use cut to remove the first space? – Don Draper Jun 7 '18 at 17:47
  • No, you'd use sed 's/^[[:blank:]]\+//' to remove leading whitespace. But at this point, stringing several commands together, you'd be better off with a single awk program. – glenn jackman Jun 7 '18 at 18:02

Just sort both files before comparing them

sort f1 > f1s
sort f2 > f2s
diff f1s f2s

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.