LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help editing .csv files (https://www.linuxquestions.org/questions/programming-9/help-editing-csv-files-331898/)

schneidz 06-09-2005 10:51 AM

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.
thanks,


example:
my,name,is,simon,what,is,your,name
->
my,name,is,schneidz,what,is,your,name

jonaskoelker 06-09-2005 11:14 AM

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.

hth --Jonas

schneidz 06-09-2005 11:35 AM

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.

thx anyways

jonaskoelker 06-09-2005 11:56 AM

Quote:

something like the combination of sed, awk
I just struck me: I think this is what `cut' and `paste' were meant to do.
At least I would consider *now* an opportune moment to read the man pages.

happy hacking,

Jonas

chrism01 06-09-2005 11:18 PM

well, there's this:
perl -i.orig -p -e 'filtercmd' file1 file2 ...
eg
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
or
sed -i -e 's/DROP TABLE/DROP TABLE IF EXISTS/' myfile.sql

schneidz 06-10-2005 12:57 PM

i didnt try chriss' approach. here's mine it is messy but it works:

#!/bin/sh

# schneidz

for csv in *.csv
do
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
done

'zeropad.x' is mentioned in this post.

http://www.linuxquestions.org/questi...hreadid=327071

thanks all,

eddiebaby1023 06-11-2005 12:32 PM

Re: help editing .csv files
 
Quote:

Originally posted by schneidz
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.
thanks,


example:
my,name,is,simon,what,is,your,name
->
my,name,is,schneidz,what,is,your,name

If the string is always simon then:
Code:

#!/usr/bin/perl -pi.bak -w
s/,simon,/,schneidz,/

in an executable file will do it. It'll preserve the original file with a .bak suffix. Your solution is certainly interesting, though!

rickh 06-11-2005 04:09 PM

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 09:37 AM.