help editing .csv files
i would like help editing variable length .csv files. since i want to save over the original file, i dont think sed-awk will work for me. i have a c program that will read a single column file, edit it, and write to another file but i have no idea how to edit it in 'real-time'. the column i want to change will always be in 4th column.
any suggestions will be appreciated.
the basic idea (if you want your code to be portable) is
1) slurp the file (= read into memory)
2) edit in-memory
3) save to disk
I think it's possible to tell the kernel to tell the filesystem to modify in-place, but depending on the fs, it might or might not like changing the size. I wouldn't have a whelk's idea in a supernova as to how, though.
this is kinda' specific although i was surprised i didnt find anything similar in the forums.
i think i will stub together a shell script. something like the combination of sed, awk and my zero-pad program and save about five or six temp files.
At least I would consider *now* an opportune moment to read the man pages.
well, there's this:
perl -i.orig -p -e 'filtercmd' file1 file2 ...
perl -i.orig -p -e 's/DATE/localtime/e' file1
makes a backup of orig file; to ignore backup, use
perl -i -p -e 's/DATE/localtime/e' file1
sed -i -e 's/DROP TABLE/DROP TABLE IF EXISTS/' myfile.sql
i didnt try chriss' approach. here's mine it is messy but it works:
for csv in *.csv
cut -d , -f -6 $csv > $csv.tmp1
cut -d , -f 7 $csv > $csv.tmp2
cut -d , -f 8- $csv > $csv.tmp3
zero-pad.x $csv.tmp2 > $csv.tmp2.mod
paste -d , $csv.tmp1 $csv.tmp2.mod $csv.tmp3 > $csv
rm -rf $csv.tmp1 $csv.tmp2 $csv.tmp3 $csv.tmp2.mod
'zeropad.x' is mentioned in this post.
Re: help editing .csv files
Since the file is already comma delimited, I think what I would do is open it in gcalc or some spreadsheet and edit it there?
|All times are GMT -5. The time now is 05:17 PM.|