I need to take one file containing lines same as this fake one:,

And i need to run trough another file *.json, and find all occurrences of first "IP" and if match is found, replace it with second "IP" from first file. There is always only one exact occurrence and the format is exactly the same, of course without delimiter, at this time its ", ".

I wound what approach would be good, its my first little "script" or as some say, more complicated command. iam using linux, so bash is one way, using something like IFS=',' ; while read ; and probably some usage of sed or mabye something else? ... what iam looking for is the what exactly i need to focus on for study, since its job task and i cant afford to python my way out from the it (learn python) or study more generally scripting and eventually get the right methods.


one line from json file i need to edit, "IP" to be changed with new one matches the line above

{"id": "segmenter_509_1", "channel_id": 509, "profile": "profile1", "description": "Cinemax_HD (700 kbps)", "source": "udp://", "duration": 10, "verbose": true, "waitForSync": true, "maxseg": 5, "logging_stream": true, "user_option": "-H -1", "encrypt": false, "interleaved": true, "dummy": false, "logging": true, "loglevel": 24, "ignore-errors": -1, "enabled": true, "mux": "vas"},

Every line is almost the same, only few values are chaning, but since i have list of old and new IPs that matches always only one line, i think its ok to work with this example

  • 1
    sed is not best (or good) tool for processing json. Use a json processor. – ctrl-alt-delor Nov 20 '18 at 11:05
  • 2
    post all the needed input fragments – RomanPerekhrest Nov 20 '18 at 11:06
  • the fact its json is just mentioned since there are some delimiters that cant be used for example, other than its static config file lets say including mainly arguments for other proceses. Iam sorry but iam not sure about all needed input fragments, what you might mean - the line from another file, how it looks? – J B Nov 20 '18 at 11:09

Would this work?

awk -F", " 'NR == FNR {T[$1] = $2; next} {for (t in T) gsub (t, T[t])} 1' file[12]

It reads the "replacement pairs" from "file1" into an array, then modifies "file2"'s lines if any pattern found.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.