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

applescript to remove first line from CSV

Hi,

I receive CSV files that I then drag and drop into a Numbers spreadsheet.

The CSV files have two lines of data (which correspond to two rows of data in a Numbers spreadsheet).

The first line in each CSV is a header that I would like to delete before it goes into the spreadsheet.

I would like to write an Applescript (application) that I could drop the CSV onto, that would delete the first (header) line.

Seems like this should be simple, but I've spent the past hour searching for how to do this and I haven't found a clear answer anywhere.

If anybody could help I'd really appreciate it.

thanks.

MacBook Pro (15-inch Early 2011), OS X Yosemite (10.10), 8 GB RAM, SSD

Posted on Mar 3, 2015 4:05 PM

Reply
Question marked as Best reply

Posted on Mar 3, 2015 4:40 PM

Saved as an Applescript App, this will delete the first line of text files dropped on the App

USE WITH CAUTION and Test


on openthe_Files

repeat with i in the_Files

tell application "Finder"

if name extension of i is "csv" then

do shell script "sed -i \"\" '1 d' " & quoted form of POSIX path of i

end if

end tell

end repeat

end open

8 replies
Question marked as Best reply

Mar 3, 2015 4:40 PM in response to arthur

Saved as an Applescript App, this will delete the first line of text files dropped on the App

USE WITH CAUTION and Test


on openthe_Files

repeat with i in the_Files

tell application "Finder"

if name extension of i is "csv" then

do shell script "sed -i \"\" '1 d' " & quoted form of POSIX path of i

end if

end tell

end repeat

end open

Mar 9, 2016 6:01 AM in response to MisterWeb

You mistakenly used the Run Shell Script action, when you need Run AppleScript action. Tony's solution was AppleScript, not Bash scripting.


When you insert the Run AppleScript, it will have boilerplate code in it. Just remove that, and paste Tony's script into it.


Let's say that I have a text file (z.txt) in my home directory, and it contains the letters A - F, one letter per line. The following AppleScript do shell script invocations do what their comment says:


set pn to ((path to home folder as text) & "z.txt")


-- remove last line from file, update it in place (-i " ")

do shell script "sed -i \" \" '$d' " & (POSIX path of pn)'s quoted form

Result: A B C D E


-- remove last 3 lines from file, update it in place (-i " ").

do shell script "sed -i \" \" -n -e :a -e '1,3!{P;N;D;};N;ba' " & (POSIX path of pn)'s quoted form

Result: A B C


-- remove last 3 lines from file (\n and \z need to be escaped for AppleScript), update it in place, keeping the original file in z.txt.orig.

do shell script "perl -i.orig -0777pe's/(?:.*\\n){3}\\z//' " & (POSIX path of pn)'s quoted form

Result: A B C

Mar 10, 2016 6:27 PM in response to MisterWeb

Not as succinct as the nice solutions above, but it's worth noting that good old "plain" AppleScript does a pretty good job at this kind of thing.


User uploaded file

You just vary the starting and ending "paragraph" number. (In this example the first and last lines are removed.) That can be easier for those of us who aren't as fluent as we should be with sed and regular expressions and escaping characters for the shell and all that.


Note that this example has no error checking to make sure the file you are dropping on the Automator app is text, so use with care.


SG



on run {f}

set s to read (f) as «class utf8»

set text item delimiters to return

set s to s's paragraphs 2 thru -2 as string

set text item delimiters to ""


open for accessf with write permission


set eoffto 0


writestof


close accessf

end run

applescript to remove first line from CSV

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