вставить", " перед 2-м вхождением конкретного слова в строку в unix
Я хочу вставить ", " перед каждым появлением https, кроме первого https в приведенном ниже JSON
вход:
https://gitlab.com/pc-sa/sa-pc-pcc/-/archive/test-demo/abc-test-demo.zip?path=Documentation Assets/temp check
https://gitlab.com/pc-sa/sa-pc-pcc/-/archive/test-demo/abc-test-demo.zip?path=Documentation Assets/temp dir
https://gitlab.com/pc-sa/sa-pc-pcc/-/archive/test-demo/abc-test-demo.zip?path=Documentation Assets/temp dir
Выход:
https://gitlab.com/pc-sa/sa-pc-pcc/-/archive/test-demo/abc-test-demo.zip?path=Documentation Assets/temp check", "https://gitlab.com/pc-sa/sa-pc-pcc/-/archive/test-demo/abc-test-demo.zip?path=Documentation Assets/temp dir", "https://gitlab.com/pc-sa/sa-pc-pcc/-/archive/test-demo/abc-test-demo.zip?path=Documentation Assets/temp dir
EDIT: добавление показанных усилий OP(в комментариях) в пост здесь.
cat /tmp/mmm.txt| sed ':a;N;$!ba; s/https/\","https/2'








Ответы - вставить", " перед 2-м вхождением конкретного слова в строку в unix / insert ", " before 2nd occurrence of a particular word in string in unix

30.01.2020 07:52:39
Я бы изменил все вхождения, а затем изменил бы первое обратно:
sed -e 's/https/", "https/g' -e 's/", "https/https/'
(Ваши примеры не совсем соответствуют описанию цели, поэтому я не могу проверить это решение, но я думаю, что оно, по крайней мере, приблизительно правильно.)


30.01.2020 08:10:14
Не могли бы вы попробовать следующее (написано и протестировано с предоставленными образцами).
awk -v s1="\",\"" '{val=(val?val s1:"")$0} END{print val}' Input_file
Пояснение: добавление подробного уровня объяснения для приведенного выше кода.
awk -v s1="\",\"" ' ##Starting awk program here and creating variable s1 whose value is ","
{ ##Starting this code main BLOCK from here.
val=(val?val s1:"")$0 ##Creating variable val whose value is keep concatenating to its own value.
} ##Closing this program main BLOCK here.
END{ ##Starting END block of this program from here.
print val ##Printing variable val here.
} ##Closing this program END block here.
' Input_file ##Mentioning Input_file name here.
Чтобы сохранить выходные данные в переменную оболочки и прочитать значения из переменной оболочки попробуйте like (где var
-ваша переменная оболочки, имеющая значения, вы можете назвать ее по своему желанию):
var=$(echo "$var" | awk -v s1="\",\"" '{val=(val?val s1:"")$0} END{print val}'); echo "$var"