Any Applescript / Shell Gurus be able to help?

Discussion in 'Mac Programming' started by Mac Virgin, Jun 24, 2007.

  1. macrumors newbie

    Joined:
    Mar 27, 2006
    #1
    Hi Guys,

    I have two text files that I have created and need to add info from one of them to the second one in a "smart" way..

    eg. File 1 contains a list with 2 columns of data

    HH25 7
    XQ03 3
    XQ30 8
    XS11 6

    File 2 contains multiple lines such as

    HH25------785QFA0487PAXSYDMEL---1110744
    HH25------786QFA0029MELHKG------11107440
    HH25------781QFA0030HKGMEL------11107440
    HH25------782QFA0029MELHKG------11107440
    HH25------784QFA0128HKGSYD------11107440
    XQ03--3-----6QFA0149SYDLAX------001074404
    XQ03--3-----7QFA0012LAXSYD------001074404
    XQ30---4----5QFA0149SYDLAX------001074404
    XQ30---4----6QFA0012LAXSYD------001074404
    XS11----56785QFA0073SYDSFO------00107440
    XS11----56787QFA0074SFOSYD------00107440

    I need to be able to add the data from the second column in file 1 to every line it matches in file 2 - ie the first line of file 1 is HH25 7 - I'd want to add the 7 to every line in file 2 which has HH25 as the first 4 characters.. then add a 3 to every line which has XQ03 as the first 4 characters and so on..

    Any help would be greatly appreciated!

    Cheers,

    Nelson.
     
  2. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #2
    want to finish the data? :p
     
  3. thread starter macrumors newbie

    Joined:
    Mar 27, 2006
    #3
    What the?

    Even the attached text file is being cut off!!

    Will try again!!

    Nelson.
     
  4. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #4
    still doesn't work for me, sorries :(
     
  5. thread starter macrumors newbie

    Joined:
    Mar 27, 2006
    #5
    See top post..

    Please see the first post - all fixed now...

    Cheers,

    Nelson.
     
  6. thread starter macrumors newbie

    Joined:
    Mar 27, 2006
    #6
    Solved

    Hi Guys,

    Sorted the problem out using a couple of nested repeat loops - I have a lot less hair now, but the job is done!!

    Cheers,

    Nelson.
     
  7. macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #7
    I was just thinking Fortran and keyed file access. :)

    AS/shell hmm... could be done. Damn slow though

    grep, grep -v
     
  8. thread starter macrumors newbie

    Joined:
    Mar 27, 2006
    #8
    Hi Mongo,

    If only I were a smarter man....

    I only have to process about 10 or so lines so speed wasn't such an issue - this time! And it is part of an overall larger AS.. which is why I was hoping for an AS / shell solution...

    Cheers,

    Nelson..
     
  9. macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #9
    Code:
    set changetext to "HH25 7
    XQ03 3
    XQ30 8
    XS11 6"
    
    set basetext to "HH25------785QFA0487PAXSYDMEL---1110744
    HH25------786QFA0029MELHKG------11107440
    HH25------781QFA0030HKGMEL------11107440
    HH25------782QFA0029MELHKG------11107440
    HH25------784QFA0128HKGSYD------11107440
    XQ03--3-----6QFA0149SYDLAX------001074404
    XQ03--3-----7QFA0012LAXSYD------001074404
    XQ30---4----5QFA0149SYDLAX------001074404
    XQ30---4----6QFA0012LAXSYD------001074404
    XS11----56785QFA0073SYDSFO------00107440
    XS11----56787QFA0074SFOSYD------00107440"
    
    set baselist to every paragraph of basetext
    set changelist to every paragraph of changetext
    repeat with aChange in changelist
    	set textKey to word 1 of aChange
    	set insertText to word 2 of aChange
    	repeat with aline in baselist
    		if aline begins with textKey then set contents of aline to contents of aline & insertText
    	end repeat
    end repeat
    set tid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ASCII character 10
    set outtext to baselist as string
    set AppleScript's text item delimiters to tid
    return outtext
    pretty snappy
     
  10. thread starter macrumors newbie

    Joined:
    Mar 27, 2006
    #10
    Thanks!

    Hi Mongo,

    Don't you just love it when you get something working, you think you're all clever, and then someone posts a script which does it with WAY fewer lines of code??? :D

    Seriously though, thanks! I still have a LOT to learn...

    Cheers,

    Nelson..
     
  11. macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #11
    One Important thing to notice in my code is the line where I set the contents of aline to the replaced string. This keeps the list being essentially the same and allows you to do the replacements in place and not have to rebuild the list.
     
  12. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #12
    I used to scoff at AppleScript as a play language, but it's pretty wondrous. After using it a while, I still think it's goofy in many ways, as a programming language. I certainly don't agree with all the design choices they made, and there are some basic features I wish were built-in that aren't (like some string and numeric functions). But still, it can do amazing things that no other programming environment can, or would be extraordinarily difficult with another.
     
  13. thread starter macrumors newbie

    Joined:
    Mar 27, 2006
    #13
    Mongo - Thanks - makes a lot of sense.. I had basically done the same thing but not as "neatly"..

    HiRez - whilst not the most elegant language around I'm sure, I've found AS to be pretty good at geting things done on the Mac.. Even at my basic stage though I've found a few shortcomings with it, but by being able to execute shell commands have been able to get around most of them.. and hoping as I get more proficient to go back and rewrite some of the code to be a little more efficient!! It's been a triple faceted problem for me - new'ish to the Mac, beginner with AS, and never played with unix before getting this Mac!!

    Loving the challenge though..

    Cheers,

    Nelson.
     
  14. macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #14
    Its lispy. Thats one of the things that a lot of people have issues wrapping their brains around. In a lot of ways C is goofy. Its a different way of thinking. AS is OOP from the ground up. String processing stuff is one thing that I would like to see more and better of in AS.
     

Share This Page