CSV: Strip header, convert to Asnii and then merge?

Hi all, I did some scouring and didn’t come up with anything. I’m a newbie to applescript so I though I’d see if somebody was feeling generous.

I have a folder full of CSV files. I think they are utf-2 right now. I need to strip the header row, convert to ANSII and then merge these files…

Where would I start with something like this?

Thanks!

Model: macbook pro
AppleScript: ??
Browser: Firefox 43.0
Operating System: Mac OS X (10.9)

Bash comes with great tools for these kind of jobs so I would use tail for this command because it’s really fast.

UTF2 is deprecated and unsupported on the Mac OS X platform, it’s successor is UTF8 so I guess UTF8 will suffice to convert UTF2 to another format. The main difference is that UTF2 is 1 or 2 bytes while UTF-8 supports up to four byte characters. But I could be totally wrong on this.

I’m not sure what you mean by ANSII, did you mean ANSI, ASCII (or ISO)? Converting UTF-8 to ASCII is not possible because it simply removes all extended characters so I will assume you meant ISO characters that has support for the Unicode Latin 1 supplement. When there are no UTF2 supplement characters it’s already ASCII and doesn’t need converting at all.

The script would look something like:

set theFolder to POSIX path of (choose folder)
set theFileNames to paragraphs of (do shell script "ls " & quoted form of theFolder & " | grep '\\.csv$'")
if (count of theFileNames) = 0 then return
set theNewFile to theFolder & "combined.csv"
do shell script "iconv -f UTF8 -t ISO-8859-1 " & quoted form of (theFolder & item 1 of theFileNames) & " > " & quoted form of theNewFile
if (count of theFileNames) = 1 then return
repeat with i from 2 to count of theFileNames
	do shell script "echo \"\" >> " & quoted form of theNewFile
	do shell script "tail -n +2 " & quoted form of (theFolder & item i of theFileNames) & " | iconv -f UTF8 -t ISO-8859-1  >> " & quoted form of theNewFile
end repeat

I just wrote such a thing day before yesterday.

[format]#!/usr/bin/env bash
csvDir=~/‘test_directory/Jon_Test_Folder/’;
cd “$csvDir”;
cat *.csv > consolidatedCsvText.csv;
headerText=$(sed -n ‘1p’ consolidatedCsvText.csv);
sed -i ‘’ “/$headerText/d” consolidatedCsvText.csv;
[/format]

It looks like DJ has the conversion issue well covered.


Chris


{ MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11.3 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯