Skip to content
Advertisement

find matching text and replace next line in yml

I’m trying to find a line in a yml configuration file and replace the next line with a specific value. I tried sed, but it seems it is not replacing or not able to find the pattern. Below is the snippit of that yml file

JavaScript

I want to change port value to 14081 for applicationConnectors as there is another port exists for adminConnectors After the script execution it should look like:

JavaScript

I have tried below code:

JavaScript

but it seems this code is not replacing anything.

Advertisement

Answer

sed is best for s/old/new, that is all. For anything else just use awk for clarity, portability, robustness, etc. Look:

JavaScript

Want to change acceptQueueSize: to 17 instead? It’s the same script with just different variable values:

JavaScript

Only try that with your currently accepted sed solution if you enjoy counting ns :-). Note also that this will work no matter what order the lines appear within each record since it keys off the name port rather than assuming that will appear some specific number of lines after applicationConnectors:. Finally, this will work even if the strings you’re searching for or replacing with contain RE metachars (e.g. .), backreference chars (e.g. 1 or &), or sed delimiters (e.g. /).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement