2

2 text files. I have to find the uniq values from the 2 text files and take the output in 3rd file with the filename along with uniq values. For eg a.txt

1
4
3

b.txt

2
1
7

Output

File.out
b.txt 2
a.txt 3
a.txt 4
b.txt 7

I tried the below command

cat a.txt b.txt | sort | uniq -u >>file.out

But it is printing only values, not getting file names

  • Can you explain how to determine the order of lines in the output file? Sorted numerically? – Mark Plotnick Nov 3 '18 at 16:50
  • Order is not needed in output file.. I need filename along with uniq values in output file . When I cat output file I want to know the uniq values are from a.txt or b.txt. – Kumar Nov 3 '18 at 17:06
4

You could prepend the filenames using Awk, then sort + uniq the second field:

$ awk '{print FILENAME,$0}' a.txt b.txt | sort -k2 | uniq -uf1
b.txt 2
a.txt 3
a.txt 4
b.txt 7

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.