Will
Will

Reputation: 169

Writing on a CSV in Applescript (Using terminal shell)

Just trying to write onto an CSV file that I choose from a dialog box. For some reason, this isn't working. Can anyone help me out? Thanks!

set theFile to (choose file)
do shell script "awk -F, '$1 ~ /Line 2/{$22=\"hello\"} 1'  & quoted form of POSIX path of theFile & > /tmp/a"

EDIT: I just want to write on a chosen CSV on a particular row/column (in this case row 2, column 22). At the moment it's not throwing any error but not writing on the chosen CSV either, displaying the output "" –

Upvotes: 2

Views: 313

Answers (3)

jackjr300
jackjr300

Reputation: 7191

Your command write the output of the awk command to a file named "a" in the "tmp" folder (this folder is hidden), so the output of the do shell script is ""

Your command edit lines where the content of the first field is equal to "Line 2".

  • To edit the second line, use NR==2

    To edit line 1 through 5, use NR<6

    To edit line 10 through 15, use NR>9 && NR<16

Standard awk on OS X cannot edit the file in place, so you must write the output to a temporary file and use the mv command to overwrite the original file, but its possible with GNU awk 4.1.0... --> gawk -i inplace


To edit the second line of some file (the line ending in the CSV file must be a line feed), use this:

set theFile to quoted form of POSIX path of (choose file)
set tempFile to theFile & ".tmp123"
do shell script "awk -F, 'NR==2{$22=\"hello\";}1' OFS=, " & theFile & " >" & tempFile & "&& mv -f " & tempFile & " " & theFile

if the line ending in the CSV file is a carriage return, use this:

do shell script "awk -F, 'NR==2{$22=\"hello\";}1' RS='\\r' ORS='\\r' OFS=, " & theFile & " >" & tempFile & "&& mv -f " & tempFile & " " & theFile

Update

Here's an example how to put an AppleScript variable (m) into the command.

set m to 6 -- a number
do shell script "awk -F, 'NR<" & m & "{$22=\"hello\";}1' RS='\\r' ORS='\\r' OFS=, " & theFile & " >" & tempFile & "&& mv -f " & tempFile & " " & theFile

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207465

If your main intention is really to run a bash+awk script, you can do things the other way around...

Instead of starting in Applescript and trying to run a shell and awk, you could start in the shell and invoke Applescript to choose a file and then carry on in the shell and run awk.

Like this:

#!/bin/bash
file=$( osascript -e 'set theFile to (POSIX path of (choose file))' )
awk '1' "$file"

So, you get a bash script that invokes Applescript, rather than an Applescript that invokes bash.

Note that 1 is just true in an awk script, so awk will just do its default thing, which is print the current line - obviously you would put your own awk commands in there.

Upvotes: 0

ShooTerKo
ShooTerKo

Reputation: 2282

I think the use of quoted form is wrong here. You have to interrupt the AppleScript string and add it between your string parts. Try this:

set theFile to (choose file)
do shell script "awk -F, '$1 ~ /Line 2/{$22=\"hello\"} 1' " & quoted form of POSIX path of theFile & " > /tmp/a"

If the rest of your shell command is fine, it should fix it...

Have fun, Michael / Hamburg

Upvotes: 1

Related Questions