Professional Web Applications Themes

Comparing two files - Linux / Unix Administration

All, I'm trying to compare files using <comm file1 file2>. This works great, it gives me separate columns. First column is text only in file1, column two is text only in file2 and text that is in both file1 & file2 in column 3. My problem is, when there are multiple occurrences of the same text in file2, and it also appears in file1, it appears both in column 2 & 3. My question is, how would I go about getting rid of the extra occurrences in column two but still show the text in column 3. Attached is a ...

  1. #1

    Default Comparing two files

    All,

    I'm trying to compare files using <comm file1 file2>. This works great, it gives me separate
    columns. First column is text only in file1, column two is text only in file2 and text that is in
    both file1 & file2 in column 3. My problem is, when there are multiple occurrences of the same text
    in file2, and it also appears in file1, it appears both in column 2 & 3.

    My question is, how would I go about getting rid of the extra occurrences in column two but still
    show the text in column 3.

    Attached is a small example of the output after I compared the two files. The full result is approx.
    10,000 lines.


    CU1289DLL1
    CU1289KSC1
    CU1291KSC1
    CU1292KSC1
    CU1293KSC1
    CU1294KSC1
    CU1295KSC1
    CU1296KSC1
    CU1296KSC1
    CU1300KSC1
    CU1302KSC1
    CU1304HUD1
    CU1304HUD1
    CU1304HUD1
    CU1304KSC1
    CU1305KSC1
    CU1306KSC1
    CU1307KSC1
    CU1308HUD1
    CU1308HUD1
    CU1308HUD1
    CU1308HUD1
    CU1309HRN1

    Thanks in advance

    B~


    BradS Guest

  2. #2

    Default Re: Comparing two files

    2005-01-31, 13:58(+00), BradS: 
    [...] [your lines are too long]

    What about running your files through uniq to have distinct
    occurences?

    comm <(uniq < file1) <(uniq < file2)

    for instance (note that you need bash or zsh for that type of
    command, ksh may also work but that would depend on your
    operating system).

    (If your system has /dev/fd/<n> files, you can also do
    uniq < file1 | { uniq < file2 | comm /dev/fd/3 -; } 3<&0
    if note, you can use a named pipe or temp file).

    If:

    file1 file2
    a a
    a a
    b a
    b d
    c e

    The result would be:
    a
    b
    c
    d
    e


    --
    Stéphane
    Stephane Guest

  3. #3

    Default Re: Comparing two files


    "Stephane CHAZELAS" <invalid> wrote in message
    news:is.invalid... 
    > What about running your files through uniq to have distinct
    > occurences?
    >
    > comm <(uniq < file1) <(uniq < file2)
    >
    > for instance (note that you need bash or zsh for that type of
    > command, ksh may also work but that would depend on your
    > operating system).
    >
    > (If your system has /dev/fd/<n> files, you can also do
    > uniq < file1 | { uniq < file2 | comm /dev/fd/3 -; } 3<&0
    > if note, you can use a named pipe or temp file).
    >
    > If:
    >
    > file1 file2
    > a a
    > a a
    > b a
    > b d
    > c e
    >
    > The result would be:
    > a
    > b
    > c
    > d
    > e
    >
    >
    > --
    > Stéphane[/ref]

    That worked in bash. Thank you!

    B~


    BradS Guest

Similar Threads

  1. Need help comparing lines in two files
    By Stuart Clemons in forum PERL Beginners
    Replies: 7
    Last Post: January 23rd, 01:24 PM
  2. [PHP] comparing xml files, removing some html tags
    By Jabber@Raditha.Com in forum PHP Development
    Replies: 0
    Last Post: August 21st, 04:21 AM
  3. comparing xml files, removing some html tags
    By Robert Mena in forum PHP Development
    Replies: 0
    Last Post: August 18th, 02:35 AM
  4. Comparing directories and files
    By Lee in forum Windows XP/2000/ME
    Replies: 0
    Last Post: July 19th, 10:04 PM
  5. Comparing two files..
    By darkname in forum PERL Miscellaneous
    Replies: 4
    Last Post: July 12th, 11:18 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139