Find the answer to your Linux question:
Results 1 to 8 of 8
Hi all I have two text files, say for example test1.txt contains the following 20,11,1998,00,00,00,324.000000,-9978,1111 22,11,1998,00,00,00,326.000000,-9865,2222 23,11,1998,00,00,00,327.000000,-9999,3333 27,11,1998,00,00,00,331.000000,-9865,4444 test2.txt contains 19,11,1998,11,00,00,00,324.000000,-9444,5555 20,11,1998,22,00,00,00,326.000000,-9999,6666 21,11,1998,33,00,00,00,327.000000,-9223,7777 22,11,1998,44,00,00,00,331.000000,-9999,8888 The first 3 comma separated values ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    6

    Script to compare to files

    Hi all
    I have two text files, say for example
    test1.txt contains the following
    20,11,1998,00,00,00,324.000000,-9978,1111
    22,11,1998,00,00,00,326.000000,-9865,2222
    23,11,1998,00,00,00,327.000000,-9999,3333
    27,11,1998,00,00,00,331.000000,-9865,4444
    test2.txt contains
    19,11,1998,11,00,00,00,324.000000,-9444,5555
    20,11,1998,22,00,00,00,326.000000,-9999,6666
    21,11,1998,33,00,00,00,327.000000,-9223,7777
    22,11,1998,44,00,00,00,331.000000,-9999,8888

    The first 3 comma separated values shows the date.
    I want to check for the similar date in both the files and return the whole data into another file.
    Need the o/p file like this
    20,11,1998,00,00,00,324.000000,-9978,1111,22,00,00,00,326.000000,-9999,6666
    22,11,1998,00,00,00,326.000000,-9865,2222,44,00,00,00,331.000000,-9999,8888

    Guess I made the thread clear to understand
    Can any one help me out please......

  2. #2
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    What about the command cmp

    But to do this in a script you do it like this:
    Pseudo Mode On
    Open File1
    Open File2
    Do Untill EOF (File1 && File2)
    {
    Read Line File1 -> into char* Line
    Read Line File2 -> into char* Line2
    if (Line != Line2){show that its diffrent}
    }
    Close files..

    If you need help with the real script I can write it in a couple of languages.
    Perl and C++ prefer.

    Edit: I just saw that you also need with a Date.
    Then I'd pick Perl for this task.
    The perl index Function or split are quite powerfull.

    Perl is real good for Text Moding.
    New Users, please read this..
    Google first, then ask..

  3. #3
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Is this a homework (see this)

    In your case it should be:

    Code:
    awk 'NR==FNR{x[$1$2$3]=$0;next}
    $1$2$3 in x{p=x[$1$2$3]","
    sub(/[0-9]*,[0-9]*,[0-9]*,/,"")
    print p$0
    }' FS="," test1.txt test2.txt

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Posts
    6

    Thumbs up

    Thank you radoulov! U r really great!!! U really saved my soul!!! It really worked. Thanx a lot!

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Posts
    6
    This script is matching these two as same! Yet its diffrent
    31,1,1998 and 3,11,1998
    some one can fix it plz....

  6. #6
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    thats since all the commas are removed by the script.
    Im not sure but try:
    Code:
    awk 'NR==FNR{x[$1$2$3]=$0;next}
    $1$2$3 in x{p=x[$1$2$3]","
    sub(/[0-9]*,[0-9]*,[0-9]*,/,"")
    print p$0
    }' test1.txt test2.txt
    New Users, please read this..
    Google first, then ask..

  7. #7
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    awk 'NR == FNR { x[$1SUBSEP$2SUBSEP$3] = $0; next }
    $1SUBSEP$2SUBSEP$3 in x { p = x[$1SUBSEP$2SUBSEP$3]
    sub( /[0-9]*,[0-9]*,[0-9]*/, "" )
    print p $0
    }' FS="," test1.txt test2.txt

  8. #8
    Just Joined!
    Join Date
    Oct 2007
    Posts
    6
    Thanx frndz! Now i think am clear with some of the basics of awk! Thanx to radoulov and Robin

Posting Permissions

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