2

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 "" –

3
  • what is the desired behavior and what is going wrong? Commented Nov 5, 2015 at 13:21
  • 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 "" Commented Nov 5, 2015 at 15:12
  • please edit the question to include this. thanks! Commented Nov 5, 2015 at 15:57

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, the carriage return works perfectly
One last thing, is it possible to do NR<m where m is a variable calculated on run-time. If so who would I have to do to m so that it is recognized as a variable
1

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

2 Comments

Thanks for the help, but it still doesn't work. It's not throwing any error but just not writing on the CSV either. Just want to write on the line 2, column 22 on the particular file chosen.
That may be an awk problem, which I am not familiar with. But the quoted form answer is helpful, too :)
0

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.

1 Comment

Sorry, perhaps I wasn't clear, this was a function of a longer script, but I'm sure this would've worked too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.