Find the answer to your Linux question:
Results 1 to 10 of 10
Hi I need to merge two files using awk or any scripting tool. The requirement is like this File A is like this: EI3171280 38640658501 NENN2005-12-129999-12-312005-12-12HALL NANCY 344 CHENEY HIGHWAY ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5

    Merging two files

    Hi I need to merge two files using awk or any scripting tool.
    The requirement is like this

    File A is like this:
    EI3171280 38640658501 NENN2005-12-129999-12-312005-12-12HALL NANCY 344 CHENEY HIGHWAY TITUSVILLE FL32780-0000 N 1-1192515160
    PI95000M 23326685801 NENN2005-12-121924-02-172005-12-12POLING EARL W 2600-26TH AVE PARKERSBURG WV26101-0000 N 1-1192669087
    EI3212756 58007867001 NSNN2005-12-121936-09-192005-12-12CRUZ AIDA P.O. BOX 1295 KINGSHILL,ST. C VI00851 N 1-1193006037
    EI3210432 05128396601 NENN2005-12-121933-04-132005-12-12NERKO ELEANORE 60-16-76TH ST ELMHURST NY11373-2417 N 1-1192833610
    EI3206592 23560778101 NENN2005-12-121941-08-312005-12-12SLAUGHTER CARL G 452 EAST FAIRLAWN DRIVE COVINGTON VA24426-0000 N 1-1192854020
    PI25802 04738568501 NENN2005-12-121947-09-272005-12-12VLECIDES CONSTANTINE 27 WIND HILL WAY HOLMDEL NJ07733-1134 N 1-1192966250
    EI3209792 43224032301 NENN2005-12-121916-01-192005-12-12SIMPSON LOUISE E 1102 DALINE SPRINGDALE AR72764-0000 N 1-1192968873
    NI2429593 76420731101 NSNN2005-12-121974-01-282005-12-12RAMIREZ PEDRO 414 W PENNSYLVANIA TUCSON AZ85714 N 1-1193264262
    EI3309644 61719151504 NSNN2005-12-121999-02-232005-12-12SANDOVAL ANDRES 1727 ROSE AVE SANTA ROSA CA95407-0000 N 1-1193559416
    EI3308928 43065868602 NENN2005-12-121968-03-022005-12-12MILLS SR JOHN D 906 HOGAN DR MURFREESBORO TN37128-0000 N 1-1193399232
    EI3308972 46825707401 NENN2005-12-121985-03-082005-12-12LEFEVRE STEPHANIE 17175 WALNUT LANE SPIRIT LAKE IA51360-0000 N 1-1193538259
    999999 ICARE 2005-12-13 000000011
    Now file B is also similar like that.
    The highlighted items in red indicate that there are eleven records.
    I just need to merge file A with file A once again. without checking any duplicate and finally increase the count of records to 22.
    Can anybody please help me with this??
    Thanks in advanc e.
    Ratul

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If you haven't read any of these man pages, I would urge you to do this at the command line:
    Code:
    man bash
    man sed
    man awk
    man perl
    If you do not have these man pages on your system, make these google searches:
    Code:
    Linux man bash
    Linux man sed
    Linux man awk
    Linux man perl
    Don't read each of them from beginning to end; you'll get spots in front of your eyes. Just scan each man page to get a feel for what's going on.

    Then make these google searchs and follow your nose to enlightenment:
    Code:
    bash tutorial
    sed tutorial
    awk tutorial
    perl tutorial
    Once you've done this, build your solution. Don't try to write the whole thing and test it; do it in small steps, testing as you go.

    When you have specific questions about why something doesn't work, come back to us, post the code you have so far, and ask your specific question.

    Good luck!
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5
    thanks buddy.. Actually am totally new to all these..havent worked on Unix before..thanks again..let me check..I'll get back to you for sure!!

  4. #4
    Just Joined!
    Join Date
    Nov 2007
    Posts
    2
    Col1=`grep "ICARE" test.txt |awk '{print $1}'`
    Col2=`grep "ICARE" test.txt |awk '{print $2}'`
    Col3=`grep "ICARE" test.txt |awk '{print $3}'`

    count=`grep "ICARE" test.txt |awk '{printf("%09d",$4*2)}'`

    grep "NN2005" test.txt >>merged.txt
    grep "NN2005" test.txt >>merged.txt
    echo $Col1 $Col2 $Col3 $count >>merged.txt

    #echo $Col1 $Col2 $Col3 $count

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    sahrawat_amit's solution will work, with three reservations:
    1. venommaker didn't specify the order in which he wanted the lines in the merged files. There's a good chance that the order used in this solution is not exactly what he had in mind.
    2. This solution, as written exactly, will provide false results if you run it twice in a row with nothing else done in between. Try it!
    3. venommaker will learn far more by constructing a solution himself and coming to us with specific problems. (And, judging from the tone of his response, he's open to walking this path to enlightenment.)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by wje_lf View Post
    sahrawat_amit's solution will work, with three reservations:
    2. This solution, as written exactly, will provide false results if you run it twice in a row with nothing else done in between. Try it!
    It also expects both files to have the same number of lines (unsurprising since he's merging the same file twice) - better to recount with wc -l when you've merged the files.

  7. #7
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5
    guys i feel so thankful to all of you...let me try them out.. am working on them...am a little fool with Unix..its totally new to me..so whatever you say..I would take it for granted..

  8. #8
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5
    hi Wje_if, I'd love to move towards the enlightenment but the problem is that I need to deliver this code at the earliest and I'm really running short of time. So in that way I'm in a little hurry..
    Anyways a couple of information that I would want to provide is:
    1. Both files are of same pattern but could be of different lengths.
    2.There is no as such order of merging. After the first file is piped to the output, the second file could be merged.
    3.And once these files are processed, they would be deleted from the Unix box and then I'll be using Java to go forward.
    Please advise!!

    Thanks again for your cooperation

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm in a bit of a hurry, too, but show us your script and tell us how it misbehaves and I'm sure someone will step forward. :)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  10. #10
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5
    hi everyone.. this solution works well for me except for a couple of things pointed out by you guys in the previous post.

    Col1=`grep "ICARE" test.txt |awk '{print $1}'`
    Col2=`grep "ICARE" test.txt |awk '{print $2}'`
    Col3=`grep "ICARE" test.txt |awk '{print $3}'`

    count=`grep "ICARE" test.txt |awk '{printf("%09d",$4*2)}'`

    grep "NN2005" test.txt >>merged.txt
    grep "NN2005" test.txt >>merged.txt
    echo $Col1 $Col2 $Col3 $count >>merged.txt

    #echo $Col1 $Col2 $Col3 $count

    Problems are:
    This solution, as written exactly, will provide false results if you run it twice in a row with nothing else done in between.. Pointed by wje_If.
    how do i fix this??
    moreover the number of lines in two different files can be different.. how to fix that problem??

Posting Permissions

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