0

I have the below two files inside the same directory:

[admin@localhost Desktop]$ cat abc.txt 
cat
dog
zebra
giraff

[admin@localhost Desktop]$ cat def.txt 
cat
giraff
tiger

Now my requirement is to comment out the lines which are duplicate inside the files of the directory.

So for instance the word cat and giraff are already there in the file abc.txt so they should be commented out inside the file def.txt.

Can this be done inside a single command in Unix or if not possible a shell script.

I can accomplish the same thing in Python don't know how to do the same in Unix.

Many thanks for any answers.

Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
  • If the word `cat` also appears in `aaa.txt`, how do you determine which file retains the uncommented line? (I interpret your question as meaning that you want to comment the line in all but one file. If that is incorrect, please clarify.) – William Pursell Aug 12 '17 at 13:46

1 Answers1

0

When you want to comment Python style (use an #) you can use the commands beneath. Changing the comments into -- of /*...*/ should be easy.
First try with result to stdout (or make backup of def.txt).

awk 'NR==FNR {a[$0]; next} {if ($0 in a) print "# " $0; else print }' abc.txt def.txt

Explanation: see What is "NR==FNR" in awk?
When you want to edit def.txt, you can use a tmp file:

awk 'NR==FNR {a[$0]; next} 
   {if ($0 in a) print "# " $0; else print }' abc.txt def.txt > d2 && mv d2 def.txt

Another way of using a tmp file is

cp <(awk 'NR==FNR {a[$0]; next}
   {if ($0 in a) print "-- " $0; else print }' abc.txt def.txt) def.txt
Walter A
  • 19,067
  • 2
  • 23
  • 43