Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 24, 2007, 03:48 AM   #1
Mac Virgin
macrumors newbie
 
Join Date: Mar 2006
Any Applescript / Shell Gurus be able to help?

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.

Last edited by Mac Virgin; Jun 24, 2007 at 10:12 AM. Reason: Removed linked file - no longer needed...
Mac Virgin is offline   0 Reply With Quote
Old Jun 24, 2007, 03:50 AM   #2
janey
macrumors 603
 
janey's Avatar
 
Join Date: Dec 2002
Location: sunny los angeles
want to finish the data?
__________________
iPad | 15" MacBook Pro | black (iPhone 3GS|MacBook) | Mac mini server
moof!

janey is offline   0 Reply With Quote
Old Jun 24, 2007, 03:55 AM   #3
Mac Virgin
Thread Starter
macrumors newbie
 
Join Date: Mar 2006
What the?

Even the attached text file is being cut off!!

Will try again!!

Nelson.
Mac Virgin is offline   0 Reply With Quote
Old Jun 24, 2007, 03:57 AM   #4
janey
macrumors 603
 
janey's Avatar
 
Join Date: Dec 2002
Location: sunny los angeles
still doesn't work for me, sorries
__________________
iPad | 15" MacBook Pro | black (iPhone 3GS|MacBook) | Mac mini server
moof!

janey is offline   0 Reply With Quote
Old Jun 24, 2007, 03:59 AM   #5
Mac Virgin
Thread Starter
macrumors newbie
 
Join Date: Mar 2006
See top post..

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

Cheers,

Nelson.

Last edited by Mac Virgin; Jun 24, 2007 at 10:14 AM. Reason: Original post fixed up...
Mac Virgin is offline   0 Reply With Quote
Old Jun 26, 2007, 06:03 AM   #6
Mac Virgin
Thread Starter
macrumors newbie
 
Join Date: Mar 2006
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 is offline   0 Reply With Quote
Old Jun 26, 2007, 08:59 AM   #7
MongoTheGeek
macrumors 68040
 
MongoTheGeek's Avatar
 
Join Date: Sep 2003
Location: Its not so much where you are as when you are.
Send a message via MSN to MongoTheGeek Send a message via Yahoo to MongoTheGeek
Quote:
Originally Posted by Mac Virgin View Post
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.
I was just thinking Fortran and keyed file access.

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

grep, grep -v
__________________
The future ain't what it used to be.
MongoTheGeek is offline   0 Reply With Quote
Old Jun 26, 2007, 05:42 PM   #8
Mac Virgin
Thread Starter
macrumors newbie
 
Join Date: Mar 2006
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..
Mac Virgin is offline   0 Reply With Quote
Old Jun 27, 2007, 07:38 AM   #9
MongoTheGeek
macrumors 68040
 
MongoTheGeek's Avatar
 
Join Date: Sep 2003
Location: Its not so much where you are as when you are.
Send a message via MSN to MongoTheGeek Send a message via Yahoo to MongoTheGeek
Quote:
Originally Posted by Mac Virgin View Post
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
__________________
The future ain't what it used to be.
MongoTheGeek is offline   0 Reply With Quote
Old Jun 27, 2007, 08:59 AM   #10
Mac Virgin
Thread Starter
macrumors newbie
 
Join Date: Mar 2006
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???

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

Cheers,

Nelson..
Mac Virgin is offline   0 Reply With Quote
Old Jun 27, 2007, 09:29 AM   #11
MongoTheGeek
macrumors 68040
 
MongoTheGeek's Avatar
 
Join Date: Sep 2003
Location: Its not so much where you are as when you are.
Send a message via MSN to MongoTheGeek Send a message via Yahoo to MongoTheGeek
Quote:
Originally Posted by Mac Virgin View Post
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???

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.
__________________
The future ain't what it used to be.
MongoTheGeek is offline   0 Reply With Quote
Old Jun 27, 2007, 07:36 PM   #12
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Jun 27, 2007, 07:55 PM   #13
Mac Virgin
Thread Starter
macrumors newbie
 
Join Date: Mar 2006
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.
Mac Virgin is offline   0 Reply With Quote
Old Jun 27, 2007, 10:32 PM   #14
MongoTheGeek
macrumors 68040
 
MongoTheGeek's Avatar
 
Join Date: Sep 2003
Location: Its not so much where you are as when you are.
Send a message via MSN to MongoTheGeek Send a message via Yahoo to MongoTheGeek
Quote:
Originally Posted by HiRez View Post
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.
__________________
The future ain't what it used to be.
MongoTheGeek is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Any rsync gurus out there willing to help troubleshoot a shell script? pruppert OS X Mavericks (10.9) 1 Jan 14, 2014 01:21 PM
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Programming 0 Oct 28, 2013 11:38 PM
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Applications and Mac App Store 0 Oct 28, 2013 11:32 PM
Anyone know how to use AppleScript or shell-script? gba2000 Mac Applications and Mac App Store 0 Apr 23, 2013 11:14 AM
Any Tranmission Gurus out there iampaulb Mac Applications and Mac App Store 1 Jan 31, 2013 05:27 PM

Forum Jump

All times are GMT -5. The time now is 07:07 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC