Does this work?

Associate
Joined
1 Aug 2003
Posts
1,053
I'm trying to run a grep command that compares the text from two files and prints out all the names in one that do not appear in the other but am uncertain as to whether the concatenation of switches is right:

grep -F -f -v old.txt old&new.txt > just_new.txt

Thoughts?
 
Yes, but if the files have different content, then it doesn't matter how sorted they are, the diff function works great if the two files are guarenteed to have the same data at the same line, that wont be the case here.
 
And this is why you should actually put a useful amount of information when you ask a question ... each time something has been suggested you have added information which would have meant the suggestion would not have been made if included originally.

Personally then I wouldn't try and be clever and do it in one command ... there is no point when you can just write a little script which parses through the files using grep and awk (or something in perl). The whole point of scripting on Linux/unix is that ou have little commands which can be chained together to do bigger things.
 
The command you want is "comm". Read the manpage and enjoy!!

For example:

# Generate list of removed products (removed_products.txt)
comm -23 <(sort $vHOME/old_products.txt) <(sort $vHOME/new_products.txt) > $vHOME/removed_products.txt

# Generate list of added products (added_products.txt)
comm -13 <(sort $vHOME/old_products.txt) <(sort $vHOME/new_products.txt) > $vHOME/added_products.txt
 
An alternative to comm would be to use "uniq" on each file so that it removes any duplicated lines, and then loop over the lines in one of those resulting files and use grep to check for its presence in the other, i.e.

Code:
#!/bin/bash

old_file=$1
new_file=$2

uniq_file1=$(mktemp)
uniq $old_file > $uniq_file1

uniq_file2=$(mktemp)
uniq $new_file > $uniq_file2

while read line ; do
    if [ -n $line ] ; then
        if ! grep $line $uniq_file2 > /dev/null ; then
            echo $line
        fi
    fi
done < <( cat $uniq_file1 )
Not tested, but I think that would do the job (maybe!)

Note: But use comm, seriously... if there is a pre-packaged tool that does what you need then go for that!
 
Back
Top Bottom