Using Bash For Loop to Search File for Unique Line

1.3k Views Asked by At

I have a file sortedfile.txt with a list of file names. I am looking to search another file , ngfilelist.txt containing a file list for all duplicates files in sortedfile.txt.

I am using this command

 for X in `uniq -d ~/Desktop/sortedfiles.txt`; do grep "$X" ~/Desktop/ngfilelist.txt; done

What should I correct in this?

2

There are 2 best solutions below

1
On BEST ANSWER

I'd do something like this:

cat sortedfiles.txt | xargs -n1 -i{} grep '^{}$' ngfilelist.txt

Instead of using a for loop, I find easier to use xargs to run a grep for every line in sortedfiles.txt against ngfilelist.txt. The output is a list the file names found in both files.

Note that ^ and $ are used to match only complete file names since probably partial matches aren't considered as valid results to your problem.

0
On

This might work for you:

 awk 'FNR==NR{f[$1]++;next}$1 in f && f[$1]>1{print}' sortedfiles.txt ngfilelist.txt