LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-09-2005, 10:51 AM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,088

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
Question 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
 
Old 06-09-2005, 11:14 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
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
 
Old 06-09-2005, 11:35 AM   #3
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,088

Original Poster
Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
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
 
Old 06-09-2005, 11:56 AM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
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
 
Old 06-09-2005, 11:18 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
Old 06-10-2005, 12:57 PM   #6
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,088

Original Poster
Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
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,
 
Old 06-11-2005, 12:32 PM   #7
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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!
 
Old 06-11-2005, 04:09 PM   #8
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Rep: Reputation: 60
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
editing files zexter Linux - Software 2 11-05-2004 11:27 AM
editing files clemrick Linux - Newbie 4 11-01-2004 11:14 PM
convert CSV (TEXT) files to UTF-16 cccc Programming 1 07-01-2004 01:54 AM
Help with editing files BajaNick Linux - General 6 06-13-2004 12:53 AM
Editing files Hone101 Linux - General 3 03-22-2003 12:47 PM


All times are GMT -5. The time now is 12:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration