sowrdking
sowrdking

Reputation: 239

how to insert string in continuous pattern in Linux

I want to insert string between symbol but the symbol is continuous. Like ';;;;;;;;;;;'

I can use echo ';;;;;;;;;;' | sed 's/\;\;/\;na\;/g', but the output will be ;na;;na;;na;;na;;na;.

What I want is ;na;na;na;na;na;na;na;na;na;na;.

[Update more specific question]

Like 'xx;;string;;;string;;string;;;;'
How can I turn it into xx;na;string;na;na;string;na;string;na;na;na;

I want all empty space between ";;" to have na in it. Is there any command it will work? Is there any command it will work?

Upvotes: 0

Views: 90

Answers (4)

αғsнιη
αғsнιη

Reputation: 2771

Another awk approach.

awk '{i=0; while(i++<2)gsub(/;;/,";na;")}1'

Or using sed:

sed ':l;s/;;/;na;/;tl'

Upvotes: 2

Rahul Verma
Rahul Verma

Reputation: 3089

using awk

$ echo "xx;;string;;;string;;string;;;;" | awk -v FS="" '{ for(i=1; i<=NF; i++) if ($i==";" && $(i-1)==";") printf "na"$i; else printf $i;  printf RS}'
xx;na;string;na;na;string;na;string;na;na;na;

Upvotes: 0

Kalanidhi
Kalanidhi

Reputation: 5092

Try this sed method also

sed 's/;/&na/g;s/$/;/' <<< ';;;;;;;;;;'

Output:

;na;na;na;na;na;na;na;na;na;na;

Explanation:

s/;/&na/g - append na in each ;
s/$/;/ - add ; in end of the line , so that it will fulfill the requirment

Upvotes: 3

Red Cricket
Red Cricket

Reputation: 10480

Why not just do ...

$ echo ';;;;;;;;;;' | sed 's/\;/\;na/g'
;na;na;na;na;na;na;na;na;na;na

If you need the trailing ; do ...

$ echo `echo ';;;;;;;;;;' | sed 's/\;/\;na/g'`\;
;na;na;na;na;na;na;na;na;na;na;

Upvotes: 0

Related Questions