Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Combine 2 lists via applescript or shell.

Hi I’m looking for someone with help with a script that I want to compile.


I have 2 CSV I need to join together, to help form a list, its something that I can do (non macro) in excel but is long winded way to merge them together.


Both files contain a list, one item per line.

Each line needs to be joined to the line of the other plus some extra input. “ _”


If the list in each do not have them same number of lines then it will not work and so needs to end.


For example


FileA.txt


Cousins

Atkins

Tremain

Chalk


FileB.txt

Steven

Frank

Barry

Phil


In Excel I was using something similar to this

=A1&" _"&[Workbook2]Sheet1!$A$1


OUTPUT FILE LIST.CSV should then look something like this.


Cousins, Cousins _Steven,

Atkins, Atkins _Frank,

Tremain, Tremain _Barry,

Chalk, Chalk _Phil,



Hope that makes sense.

Mac Pro, OS X Yosemite (10.10.2)

Posted on Oct 8, 2015 8:56 AM

Reply
Question marked as Best reply

Posted on Oct 8, 2015 9:51 AM

The UNIX paste(1) command and Perl appear to do the job.

paste FileA.txt FileB.txt | perl -ne '/^(\w+)\s+(\w+)$/ && print "$1, $1 _$2,\n"' > FileC.txt

Contents of FileC.txt:

Cousins, Cousins _Steven,

Atkins, Atkins _Frank,

Tremain, Tremain _Barry,

Chalk, Chalk _Phil,

7 replies

Oct 9, 2015 8:08 AM in response to MattJayC

Ok I have worked out why,


On the second sheet there are spaces on the line.


I need to get it to look at the whole line and paste. Is this still possible?


For example


FileA.txt


Cousins

Atkins

Tremain

Chalk


FileB.txt

Steven S

Frank G

Barry H

Phil J


It needs to look like this

Cousins, Cousins _Steven S,

Atkins, Atkins _Frank G,

Tremain, Tremain _Barry H,

Chalk, Chalk _Phil J,

Oct 9, 2015 10:01 AM in response to MattJayC

Based on your new criterion, I have changed the $2 capture to include the middle initial separated from the first name by a single space.


paste FileA.txt FileB.txt | perl -ne '/^(\w+)\s+(\w+\s[a-zA-Z])$/ && print "$1, $1 _$2,\n"' > FileC.txt

Contents of FileC.txt:

Cousins, Cousins _Steven S,

Atkins, Atkins _Frank G,

Tremain, Tremain _Barry H,

Chalk, Chalk _Phil J,

Combine 2 lists via applescript or shell.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.