Como inserir uma string entre 2 consecutivos?

1

Eu quero inserir uma string possivelmente N.A. entre dois , consecutivos.

Esta é minha string.

5 Local A2A SMS valid for 1 day , 10 A2A SMS valid for 2 days , 10 Local A2A SMS for 2 days only , Daily 5 Local A2A night SMS for 2 days , daily 5 local A2A night SMS for 7 days ,,,,, 5 local A2A night SMS for 2 days ,,, Outgoing Rs 40/min and Incoming Rs 30/min. Applicable only on selected countries and Operators ,,, Outgoing Rs 30/min and Incoming Rs 20/min. Applicable only on selected countries and Operators ,,,,,,,,, 5 Local night A2A SMS(11PM to 6 AM-2 days) ,, 5 Night A2A SMS for 1 Day ,, Roaming Tariff - Incoming Free, Outgoing local @ 80p/min, STD @1.15Rs/min with Talk Time 25 in main A/c ,,, 5 night local A2A sms (2 Days) ,,, Top up TT in Main a/c 95.79 + 5 A2A Night SMS. SMS valid for 2 days ,,, 300 MB 3G/4G Data  , 138.51 TT(unltd val)+5 nite A2ASMS, SMS valid for 2 days , Roaming Tariff - Incoming Free, Outgoing local @ 80p/min, STD @1.15Rs/min with Talk Time 120 in main A/c ,, 500 MB 3G/4G data for 30 Days  , 2 SMS valid for 2 days , 1Gb 3G/4G and 100TT , 1.5GB 3G/4G and 100TT , 2GB 3G/4G and 100TT , 2.5GB 3G/4G and 100TT , 3GB 3G/4G and 100TT , 3.5GB 3G/4G and 100TT , 4GB 3G/4G and 100TT , 200 MB 3G/4G Data , Roaming Tariff - Incoming Free, Outgoing local @ 80p/min,

Por exemplo, existem 5 vírgulas consecutivas, 3 consecutivas, 6 consecutivas na string. Quero inserir N.A. entre cada vírgula.

,,,,, a ser convertido em ,N.A.,N.A,N.A.,N.A., etc.

    
por penta 24.07.2016 / 11:26

2 respostas

4

Com o GNU sed:

echo "your string with commas" | sed ':a;s/,,/,N.A.,/g;ta'

:a: label with name a

s/,,/,N.A.,/g: search and replace all (g) ,, by ,N.A.,

ta: If s/// has done a successful substitution, then branch to label a

    
por 24.07.2016 / 11:40
2

Você pode fazer isso no bash assim:

 a="5 Local A2A SMS valid for 1 day , 10 A2A SMS valid for 2 days , 10 Local A2A SMS for 2 days only , Daily 5 Local A2A night SMS for 2 days , daily 5 local A2A night SMS for 7 days ,,,,, 5 local A2A night SMS for 2 days ,,, Outgoing Rs 40/min and Incoming Rs 30/min. Applicable only on selected countries and Operators ,,, Outgoing Rs 30/min and Incoming Rs 20/min. Applicable only on selected countries and Operators ,,,,,,,,, 5 Local night A2A SMS(11PM to 6 AM-2 days) ,, 5 Night A2A SMS for 1 Day ,, Roaming Tariff - Incoming Free, Outgoing local @ 80p/min, STD @1.15Rs/min with Talk Time 25 in main A/c ,,, 5 night local A2A sms (2 Days) ,,, Top up TT in Main a/c 95.79 + 5 A2A Night SMS. SMS valid for 2 days ,,, 300 MB 3G/4G Data  , 138.51 TT(unltd val)+5 nite A2ASMS, SMS valid for 2 days , Roaming Tariff - Incoming Free, Outgoing local @ 80p/min, STD @1.15Rs/min with Talk Time 120 in main A/c ,, 500 MB 3G/4G data for 30 Days  , 2 SMS valid for 2 days , 1Gb 3G/4G and 100TT , 1.5GB 3G/4G and 100TT , 2GB 3G/4G and 100TT , 2.5GB 3G/4G and 100TT , 3GB 3G/4G and 100TT , 3.5GB 3G/4G and 100TT , 4GB 3G/4G and 100TT , 200 MB 3G/4G Data , Roaming Tariff - Incoming Free, Outgoing local @ 80p/min,"
 b=
 while [ "$b" != "$a" ]
 do 
   b="$a"
   a="${a/,,/,N.A.,}"
 done
 echo "$a"
    
por 24.07.2016 / 11:53