LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find and replace (probably with sed) or some other linux commands (https://www.linuxquestions.org/questions/linux-newbie-8/find-and-replace-probably-with-sed-or-some-other-linux-commands-775441/)

dbgeek 12-13-2009 07:35 PM

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

ghostdog74 12-13-2009 07:45 PM

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


dbgeek 12-14-2009 05:57 AM

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


All times are GMT -5. The time now is 04:03 PM.