I have a txt (ip.txt) file that contains IP addresess. I group them under TCP and UDP. File excerpt is below:#TCP162.298.38.930162.298.38.931162.298.38.932#UDP162.298.38.930162.298.38.931162.298.38.932I have a specific IP (162.298.38.929) to add both by bash script into this ip.txt under TCP and UDP lines so it should like this.#TCP162.298.38.929162.298.38.930162.298.38.931162.298.38.932#UDP162.298.38.929162.298.38.930162.298.38.931162.298.38.932How can I achieve this in bash script? Line number is not usable for my case because #TCP and #UDP line number can change because I add lots of comments into the file. Bash Script should search and add the IP both under TCP and UDP.
Adding Text After Specific Line of File in Bash Script
bash;shell script
Use GNU Sed's Append ActionAssuming both sections have 162.298.38.932, and that you want to append the other IP address immediately below both entries, then the following sed script does the job.sed '/162\.298\.38\.932/a\162.298.38.929' ip.txt