2つの連続した文字列の間に文字列を挿入するには?

2つの連続した文字列の間に文字列を挿入するには?

N.A.2つの連続した文字列の間に文字列を挿入したいと思います,

これは私のロープです。

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,

たとえば、文字列には5つのコンマ、3つの連続、6つの連続があります。N.A.各カンマの間に挿入したいです。

,,,,,,N.A.,N.A,N.A.,N.A.,などに変換します。

答え1

GNU sedの使用:

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

:a:aというタグ

s/,,/,N.A.,/gg:()をすべて検索して次のよう,,に置き換えます。,N.A.,

tas///交換が成功したらラベルに分岐します。a

答え2

Bashでこれを行うことができます。

 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"

関連情報