Replace the character

Hi All,

I have a file with below format, columns are separated by ,

1575957.86,2186027.51,3092745.98,4320856.55,2536012.28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,,,,,,,,,,,,,,,

I want to replace all

,, 

with

 ,0,

However if I tried
awk '{gsub(/[,,]+/,",0,")}1'
then my o/p looks what I expect for end of columns where no value are present but it add extra ZERO to the other columns as well. which I don't want.

1575957.86,0,2186027.51,0,3092745.98,0,4320856.55,0,2536012.28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

If I use sed -e "s/,,/,0,/g" then it replace all alternate columns of

,,

to

 ,0,
1575957.86,2186027.51,3092745.98,4320856.55,2536012.28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,,0,,0,,0,,0,,0,,0,,

does anyone has any idea?

Hello manas_ranjan,

Could you please try following and let me know if this helps(not tested though).

awk -F, '{for(i=1;i<=NF;i++){A=A?A OFS $i:$i};print A;A=""}' OFS=",0,"  Input_file

Thanks,
R. Singh

Thanks Ravinder, but No it's not working.........it's also replacing every alternate position in last columns to ZERO like SED and adding ZERO to non blank columns like first, 2nd etc

1575957.86,0,2186027.51,0,3092745.98,0,4320856.55,0,2536012.28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,,0,,0,,0,,0,,0,,0,,0,,0,,0,,0,,0,,0,,0,,0,,0,

How about

awk '{gsub(/,[^0-9.]/,",0")}1' file

or

sed  's/,[^0-9]/,0/g' file

---------- Post updated at 13:55 ---------- Previous update was at 13:40 ----------

No - not good. Try - for even field count -

awk '{gsub(/,,/,",0,0")} 1'  file

or

sed 's/,,/,0,0/g' file

Hi Rudy,

Thanks. But my apologies, that I have an extra column which is character, so if I apply the formula then it amend 0 to the character,

original file

DummyCPKookmin,ENE,1575957.86,2186027.51,3092745.98,4320856.55,2536012.28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,,,,,,,,,,,,,,,

now after the change o/p looks like

0ummyCPKookmin,0NE,1575957.86,2186027.51,3092745.98,4320856.55,2536012.28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

I only wants ,, to be replaced by ,0,
I'm sorry again, for missing out those character columns

---------- Post updated at 07:58 AM ---------- Previous update was at 07:56 AM ----------

sorry i have not checked your 2nd comments.
Please ignore my response.

---------- Post updated at 07:58 AM ---------- Previous update was at 07:58 AM ----------

sorry i have not checked your 2nd comments.
Please ignore my response.

Not good, either. Try

sed ':L; s/,,/,0,/; tL' file
1 Like

thanks Rudy 2nd option is working fine.

Better:

sed ':L; s/,,/,0,/g; tL; s/,$/,0/' file

manas_ranjan, in case you are interested: the reason why your initial solution didn't work and why RudiC's second solution does is because replacements - regardless of sed or awk - are not done consecutively but simultaneously. Consider the following example string:

,,,

By your regexp you replace the (marked blue) part by the (marked red) replacement:

Before: ",,,"      After: ",0,,"

The resulting ",," will not be replaced because for this to work the whole line would have to be re-evaluated. But during the first pass only "," remains to be evaluated and you asked for only ",," to be replaced, not ",".

RudiC's solution worked because it sets up a loop: after replacing the first occurrence the string looks:

,0,,

Then, the command tL is executed. "t" will branch to the marker "L" (which is at the beginning: :L ) if the last "s/..."-command did indeed replace something. If not, it does nothing. Because RudiC has used the "g"-option only two passes at max are necessary:

String: ",,,,,,,,,,,,,,,,"
pass 1: ",0,,0,,0,,0,,0,,0,,0,,0," -> return to "L" marker
pass 2: ",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"  -> return to "L" marker
pass 3: nothing changed, end of loop ("," at the end is changed by the last substitution to ",0")

I hope this helps.

bakunin

If you would like a Perl alternative:

perl -pe 's/,(?=,)/,0/g and s/,$//' manas_ranjan.file