Thread: Diff command

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    71
    Rep Power
    13

    Diff command


    Hi,

    I am having two files. Data in both the files are as
    File 1 :

    BCSU-1 0032 WO-EX -
    BCSU-2 0034 SP-EX -
    BCSU-3 0045 WO-EX -
    BCSU-4 0046 WO-EX -

    File 2:

    BCSU-1 0032 SP-EX -
    BCSU-3 0045 WO-EX -

    I need a command which will compare these two files and give me an output as,

    BCSU-2 0034 SP-EX -
    BCSU-4 0046 WO-EX -

    In short, the output file should not contain those lines where common first word exists. I tried using diff command, but it compares the whole line instead of comparing only the first word. Please let me know how to tackle this problem.

    BR
    Nevil
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    210
    Rep Power
    31
    Not sure what kind of shell you're using. With bash or ksh, this works fine:
    Code:
    for i in `awk '{print $1}' file1`
    do
      if ! grep $i file2 >/dev/null 2>&1
      then
        grep $i file1
      fi
    
      if ! grep $i file1 >/dev/null 2>&1
      then
        grep $i file2
      fi
    done
    Don't know of any single command that will do what you want.

    If either file has more than one line that starts with "BCSU-2" (as an example) then this hack might not work properly.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    71
    Rep Power
    13
    Its worked for me. Thanks a ton.

IMN logo majestic logo threadwatch logo seochat tools logo