Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mac Virgin

macrumors newbie
Original poster
Mar 27, 2006
15
0
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.
 

Mac Virgin

macrumors newbie
Original poster
Mar 27, 2006
15
0
What the?

Even the attached text file is being cut off!!

Will try again!!

Nelson.
 

Mac Virgin

macrumors newbie
Original poster
Mar 27, 2006
15
0
See top post..

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

Cheers,

Nelson.
 

Mac Virgin

macrumors newbie
Original poster
Mar 27, 2006
15
0
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.
 

Mac Virgin

macrumors newbie
Original poster
Mar 27, 2006
15
0
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..
 

MongoTheGeek

macrumors 68040
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..

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
 

Mac Virgin

macrumors newbie
Original poster
Mar 27, 2006
15
0
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..
 

MongoTheGeek

macrumors 68040
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..

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.
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
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.
 

Mac Virgin

macrumors newbie
Original poster
Mar 27, 2006
15
0
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.
 

MongoTheGeek

macrumors 68040
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.