-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.

4
  • 2
    Read man comm. Commented Jul 20, 2016 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 . Commented Jul 20, 2016 at 8:53
  • can you please post sample content of your files, and expected behavior ? Commented Jul 20, 2016 at 8:55
  • That question is different to what I asked. That question is more generalized. Mine is more specific. Commented Jul 20, 2016 at 11:18

2 Answers 2

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.

4
  • Savior. Thanks. Works good for now. Will do more analysis and report back. Commented Jul 20, 2016 at 9:18
  • FYI, see unix.stackexchange.com/questions/296643/… for a similar question I answered last night. Commented Jul 20, 2016 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. Commented Jul 20, 2016 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. Commented Jul 21, 2016 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.

4
  • @rahul UUOC is only reason enough for making a comment, not for editing someone else's answer. Commented Jul 20, 2016 at 9:08
  • The output is - grep: illegal option -- q Usage: grep -hblcnsviw pattern file . . . Commented Jul 20, 2016 at 11:21
  • -q is used for silent. You can use above code snipped by removing -q as well. Commented Jul 20, 2016 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 Commented Jul 21, 2016 at 2:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.