Hi Everybody, I'm trying to find a way to use either awk or sed to find and replace content in a .json file using a two column csv as the source for the terms to find and their respective replacements.
Something that may look like this: - sed 's/col-1,row-1/col-2,row-1/g' input.json && sed 's/col-1,row-2/col-2,row-2/g' input.json &&
Running a sed for every pattern/replacement pair is going to get tedious. The mechanism could be improved by reading each pair into variables in a shell loop, and looping over a sed using those variables in a single command.
sed also has the issue that the s/.../ option uses some characters for syntax, so your pattern/replacement texts have to avoid having special characters which are used as sed syntax or RE operators.
Also note that sed generally reads and writes distinct files, so your sed prototype either needs to use the -i (update in situ) option every time, or to pipe all the seds together.
Awk would be a whole lot better. It can read the text pairs into an associative array, and then process the input file once, making all the relevant substitutions on each line as it goes.
Awk also has a choice of RE or plain text matching, unlike sed, and it can easily count the substitutions and report those to stderr, for that reassuring feeling.
With either awk or sed, there can be difficulties where items in your pairs overlap: e.g. s/this/that/ and s/that/other/ may do a double substitution (or not, depending on the ordering). That can be worked around in Awk reasonably easily too.
As vgersh99 notes, I will hold off on a solution until some sample data is seen.
@rtwolfe94022 wow! and you consider using sed inside awk piped to while read with the eval a worth solution?
I'd suggest you read-up on "best practices" of shell scripting. This approach is definitely NOT a "best practice" of nesting tools when the operation can be performed by either one.
Take a look here, here and here among other google-able hits for sed inside awk.
I'd also suggest formatting your code/data samples with the the proper markdown code tags.
@rtwolfe94022, you're welcome.
The comments were not intended to be offensive and/or making you leave the forums. I don't know what lead you to think so.
Furthermore, it's strange to realise that it lead you to start deleting your own posts in such numbers.