Find the answer to your Linux question:
Results 1 to 4 of 4
I have two files (each file contains one word per line). I want to compare these two files and if in the first file there is one or more unique ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    58

    find unique words in files in bash


    I have two files (each file contains one word per line). I want to compare these two files and if in the first file there is one or more unique words then I want to print the first line of a third file. I have a piece of code but it doesn’t work.
    Thanks a lot for your time.

    Code:
    For word in FILE1
    Do
    For word2 in FILE2
    Do
    
    If [ $word = $word2 ]
    Sed ‘1p’ FILE3
    Fi
    Done
    done

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    679
    I assume that you realize that the command are completely lower case and not as you have them typed. The problem that your code has is:
    1. you are looking for "common" words not "unique" words
    2. you will print the whole file with your sed command (needs the "-n" option)
    3. you will print (assuming the "-n" option) the first line each time that the if succeeds


    Given your requirements my script would be:
    Code:
    #!/bin/bash
    #
    
    if [ $(comm -32 <(sort -u file1 | tr '[:upper:]' '[:lower:]') <(sort -u file2 | tr '[:upper:]' '[:lower:]') | grep -c '^') -gt 0 ] ; then
       sed -n '1p' file3
    fi
    If you know the filters and utilities present in Unix/Linux, then script writing results in a lot less code and runs faster. If this was a homework assignment my first comments will help you out.

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    3
    Hi there,

    there are at least 2 command line tools which might help you solving your problem:

    uniq and grep

    have a look at the man pages:

    man uniq
    man grep

    specially at the '-f' option from grep, e.g.

    grep -f foo.txt bar.txt

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    679
    If you need not worry about case this is as simple as:
    Code:
    if [ $(comm -32 <(sort -u file1) <(sort -u file2) | grep -c '^') -gt 0 ] ; then
       sed -n '1p' file3
    fi

Posting Permissions

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