Find the answer to your Linux question:
Results 1 to 4 of 4
HI Everyone, I have a bash scripting question here, that's been giving me trouble.I have two files here with names in them. I need to create two new files by ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    10

    Finding lines not common in both files

    HI Everyone,

    I have a bash scripting question here, that's been giving me trouble.I have two files here with names in them. I need to create two new files by comparing these files.

    Each file will have only unique names from the file. In other words, one file contain ONLY unique names in file1. Then the next file contain only the unique names in file2.

    file1
    ############
    ruby
    heather
    ethel
    alicia
    melody
    rosene

    file2
    ############
    heather
    alicia
    carla
    ruby
    gloria


    So when the script has run, I'd have two files - unique1 & unique2.

    unique1
    ############
    melody
    rosene

    unique2
    ############
    carla
    gloria

    I'd like to stay with bash scripting if at all possible. I've tried the DIFF function, but that outputs all differences to the same file.

    Any ideas for me?

    Thanks so much,

    Dave

  2. #2
    Just Joined!
    Join Date
    Sep 2008
    Posts
    20
    grep -v -f file1 file2 > unique2

    grep -v -f file2 file1 > unique1


    btw, ethel should be in unique1, right?

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    10
    Quote Originally Posted by piwener View Post
    grep -v -f file1 file2 > unique2

    grep -v -f file2 file1 > unique1
    Now that is just the Cat's Pajamas. Works exactly like I wanted it to. Thanks a million, piwener.

    Quote Originally Posted by piwener View Post
    btw, ethel should be in unique1, right?
    Um, gulp. Guess that shows why I need a script to do this for me.

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Look at the comm command - this is what it's designed for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...