Replace the character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace the character
# 8  
Old 01-26-2016
Better:
Code:
sed ':L; s/,,/,0,/g; tL; s/,$/,0/' file

# 9  
Old 01-26-2016
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:

Code:
,,,

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

Code:
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:
Code:
,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:


Code:
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
# 10  
Old 01-26-2016
If you would like a Perl alternative:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

2. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

3. UNIX for Dummies Questions & Answers

Replace a character with other

Hi All, I am having the below sample lin in the file. '''||V_D_BOOKING_TYPE||''' || ''¬'' || '''||V_D_SHARE_DELIV_FL||''' || ''¬'' || '''|| SUBSTR(V_D_FINANCIALDATATYPE,1,1) ||''' || ''¬'' || I need to replace ''¬'' with char(172). how do i do it. PLease help. sed 's/\''¬\''/chr(172)/g'... (3 Replies)
Discussion started by: abhi_123
3 Replies

4. Shell Programming and Scripting

Replace ^Z Character

Hello, I am trying to replace all ^Z chars in my file to a space using the following command: cat <file> | tr '\32' ' ' But the above command is deleting all NULL in the file. Hence the field positions are being changed. Please let me know the reason, if anyone knows. Thanks in... (15 Replies)
Discussion started by: risshanth
15 Replies

5. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

6. Shell Programming and Scripting

replace character with '

ive tried doing sed -i 's/h/\'/g' file.txt to replace h with ' but it doesnt work:confused:. Any ideas would be greatly appreciated. (3 Replies)
Discussion started by: lyman1216
3 Replies

7. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

8. Shell Programming and Scripting

replace a character with another character

hi i have a string var=abc.ghi.jkl.mno.pqr now i need to replace .(dot) with _(underscore) the result should be like "arresult=abc_def_ghi_jkl_mno_pqr" Please help (8 Replies)
Discussion started by: satish@123
8 Replies

9. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

10. UNIX for Dummies Questions & Answers

replace character with tr

Hi, i would like substitute the words containing the character "-" by any other. i've already tried the tr command: tr '\-' 'X', for example. But, this command doesn't work. It gave this: Original text: -i abcd-fe Result text: Xi abcdXfe Result desired: -i abcdXfe Thanks! ;) (3 Replies)
Discussion started by: tmxps
3 Replies
Login or Register to Ask a Question