Thursday, February 3, 2022

[SOLVED] How to use sed to replace "word{word" or "word}word"

Issue

I tried to write a bash-script to replace things like:

{"Ausstattung":"

"},{"Ausstattung":"

"}

in all CSV files of a subfolder.

This is what I am using so far:

function suche_und_ersetze() { // search and replace
   LC_ALL=C find ./subfolder -type f ! -name ".DS_Store" -exec sed -i "" "s/${OLD}/${NEW}/g" {} \; 
}

OLD='{\"Ausstattung\":\"' 
NEW="#" 
suche_und_ersetze

OLD='\"},{\"Ausstattung\":\"'
NEW=" #" 
suche_und_ersetze

OLD='\"}' 
NEW="" 
suche_und_ersetze

Somehow I can't make it work. The script is replacing text without " or } or ,, but not the upper listed words.


Solution

Sorry, i should have looked into the csv with an editor:

It is ""},{""Ausstattung"":"" and not "},{"Ausstattung":"

In sed you even don't need to escape " if you use '



Answered By - Christian Fröhlich
Answer Checked By - Gilberto Lyons (WPSolving Admin)