LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-13-2009, 08:35 PM   #1
dbgeek
LQ Newbie
 
Registered: Feb 2006
Posts: 2

Rep: Reputation: 0
find and replace (probably with sed) or some other linux commands


The file I would like to to replace is almost 1GB.

Here is an example.

,A,B,,,,,C,c,,,"Test, This",,

replace to

"",A,B,"","","","",C,c,"","","Test, This","",""



Thanks in advance.

Joe
 
Old 12-13-2009, 08:45 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use gawk for big files
Code:
awk -F"," '{
    for(o=1;o<NF;o++){
        if( $o =="" ){
            $o="\042\042"
        }
        printf $o","
    }
    if ($NF==""){ $NF="\042\042" }
    print $NF
}'  file
 
Old 12-14-2009, 06:57 AM   #3
dbgeek
LQ Newbie
 
Registered: Feb 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you very much ghostdog74.

It worked well except in two scenarios. One is very rare

1. when there are more than one , in double quotes eg. "Test,, This"
2. It didn't replace comma at the end. eg. a,b,, should be a,b,"","".

Both are minor and I think I should be able to tweak the code. I appreciate your help.

Joe
 
  


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
Find several different words and replace with one using sed. Techno Guy Linux - Newbie 18 07-06-2009 08:16 AM
Multi-line find/replace with GnuWin32 Sed Question EdrickV Programming 4 04-26-2009 02:32 PM
Use sed to find and replace a url xmrkite Linux - Software 4 10-10-2007 08:20 PM
sed - find and replace command bullshit Programming 9 01-05-2006 04:25 AM
SED;find and replace;help required gd13 Programming 3 12-21-2004 07:33 AM


All times are GMT -5. The time now is 10:28 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration