sed help.

guys...

I have a file like this..

aa;sds;dsa;;das;;;das;;;;
bg;;dsa;;;sd;fd;;;sd;;sd;

each word is separated by a ";".
U can see there are consecutive more thn 1 ";" also. I want to insert 0 (zero) between them.

max consecutive no of ";" are 5.

Pl help.

thanks in advance.

if ur ans is SED. thn can i do this without renaming a file.??

regards,
Anchal.

Try this,

sed -ne 's/;;*/0/gp' /tmp/inp > /tmp/out

Thanks
Nagarajan G

sed -e "s/;;/;0;/g" input.txt

HI nagragan...
thanks for the reply.

But the command u posted replacing all my ";" with zero.

but I want to insert 0 ( zero) between them.

Pl help.

more clear I want the output as.
aa;sds;dsa;0;das;0;0;das;0;0;0;
bg;0;dsa;0;0;sd;fd;0;0;sd;0;sd;

sed -e "s/;;/;0;/g;s/;;/;0;/g" input.txt

try this...

sed -e :a -e 's/;;/;0;/;ta' input.txt

using Perl:

perl -pi -e 's/;;/;0;/g' file

Hi vino..
ur soln works only for alternate occurance ..
the output of ur sol is

aa;sds;dsa;0;das;0;;das;0;;0;
bg;0;dsa;0;;sd;fd;0;;sd;0;sd;

quintet's soln works for me...thnks..
quintet, can u pl explain what "/ta'" is doing in this...?

thanks..

:a - defines the label a
ta - If a s/// has done a successful substitution then branch to label 'a'

say.. in ya line

as ;;; as

so for the first time s/;;/;0;/;ta -> will successfully substitute to "as ;0;; as"
and it will branch to a again and will do make it "as ;0;0;as" and again branch to a.. now there is no substitution made, so will move to next line

got it... :slight_smile:
thanks u very much.. was unaware to this thing in sed

thnsk a lot..

@Yogesh Sawant : I have limitatio to use perl's... evn it isnt installed on the machine.
nyways thanks for u and every 1 for help.

Good Work

But could you please elaborate. I am not able to understand. Whats the purpose of label here?
The example is also not clear to me.
Could you please explain?

Thanks

It is very similiar to the goto statement in c/c++.

yeah.. sure

consider this example

sd;;sd;;;sd;;;;sd;;;;;sd

if we use sed with the following expression

sed -e 's/;;/;0;/g' -> the output will be sd;0;sd;0;;sd;0;;0;sd;0;;0;;sd

as u know sed is a line interperter and once a expression is found and replaced, it will start with the next character in the line.. i mean...
in ";;;" sed will match the first ";;" expression and will replace it to ";0;" and will start processing from the next character (which is the third ';' ) and thereby missing a '0' between 2nd and 3rd ';'

so in case of sed -e :a -e 's/;;/;0;/;ta'

when a branch is made back to position a, the search for expression will start from the beginning of the line rather than from the next character... so it will catch the 2nd and 3rd ;s now and will insert a zero between them...

hope this helps

The label name doesn't matters here. Am i right ?????

It doesnt matter.. you can use any name

Ok. As per my understanding the logic is as follows:

replace ;; with ;0; within a line untill all such possibilities are substituted. The line which is eligible for substitution is labelled as <Label-Name>

Please correct me if i am wrong

I wud rather say the position in the current line is labelled as <Label-Name>
since in my first -e i have just defined the label.. the position is the beginning of the line....

Ok. That seems to be more precise.

Thanks