-2

I have 2 files. names1.txt and names2.txt.

I need to know that which names are present in names1.txt but are missing in names2.txt. Then I need to store them in another file missing_names.txt.

Diff command gives the difference but it also prints lot many information. Also, I do not want to know the names which are in names2.txt but are missing in names1.txt. So its like (names1.txt - names2.txt) operation.

  • 2
    Read man comm. – Satō Katsura Jul 20 '16 at 8:51
  • 1
    Answered here : unix.stackexchange.com/questions/144623/… - downvoted because the question shows no intention of using a search engine before posting . – schaiba Jul 20 '16 at 8:53
  • can you please post sample content of your files, and expected behavior ? – Rahul Jul 20 '16 at 8:55
  • That question is different to what I asked. That question is more generalized. Mine is more specific. – romil gaurav Jul 20 '16 at 11:18
1
diff <(sort -u names2.txt) <(sort -u names1.txt) | 
  sed -n -e 's/^> //p' > missing_names.txt 

or

diff <(sort -u names1.txt) <(sort -u names2.txt) | 
    sed -n -e 's/^< //p' > missing_names.txt 

Either of those will give you ONLY the names that are in names1 but not names2.

diff (without any output-format options like -u) prints additions prefixed with > (> followed by a space) and deletions prefixed by < (< followed by a space). The sed scripts strip those from the beginning of the line if they're there and only prints modified (i.e. matching) lines.

  • Savior. Thanks. Works good for now. Will do more analysis and report back. – romil gaurav Jul 20 '16 at 9:18
  • FYI, see unix.stackexchange.com/questions/296643/… for a similar question I answered last night. – cas Jul 20 '16 at 9:24
  • The solution is not that accurate. If the line in one file is present in another file as well but at some other position then it is coming in the result. This should not happen. Sorting is working but sorting is combining few of the line. – romil gaurav Jul 20 '16 at 12:14
  • you're right about accuracy, so i edited my answer to sort -u the input files....but what do you mean by "sorting is combining few of the line"? sort doesn't combine lines. it sorts its input, and that's all it does. – cas Jul 21 '16 at 2:52
0
cat name_1.txt | while read line
do
  grep -q "$line" name_2.txt
  if [ "$?" -gt "0" ]; then
     echo "$line" >> name_3.txt
  fi
done

NOTE: Assuming name_1.txt and name_2.txt contains only names.

  • @rahul UUOC is only reason enough for making a comment, not for editing someone else's answer. – cas Jul 20 '16 at 9:08
  • The output is - grep: illegal option -- q Usage: grep -hblcnsviw pattern file . . . – romil gaurav Jul 20 '16 at 11:21
  • -q is used for silent. You can use above code snipped by removing -q as well. – SHW Jul 20 '16 at 12:00
  • 1
    If your grep doesn't have -q for quiet (no output), use grep "$line" name_2.txt > /dev/null. Also, the grep and the if lines can be combined into if ! grep -q "$line" name_2.txt ; then. Or get rid of the if entirely and use ||: grep -q "$line" name_2.txt || echo "$line" >> name_3.txt – cas Jul 21 '16 at 2:59

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.