Replace text between two strings in file using linux bash

sed replace between two patterns
sed replace everything between two words
unix sed replace between two patterns
bash script to replace text in a file
sed replace between two line numbers
sed replace characters between delimiters
replace everything between two strings
grep extract string between two delimiters

i have file "acl.txt"

 192.168.0.1
 192.168.4.5
 #start_exceptions
 192.168.3.34
 192.168.6.78
 #end_exceptions
 192.168.5.55

and another file "exceptions"

 192.168.88.88
 192.168.76.6

I need to replace everything between #start_exceptions and #end_exceptions with content of exceptions file. I have tried many solutions from this forum but none of them works.


EDITED:

Ok, if you want to retain the #start and #stop, I will revert to awk:

awk '
    BEGIN       {p=1}
    /^#start/   {print;system("cat exceptions");p=0}
    /^#end/     {p=1}
    p' acl.txt

Thanks to @fedorqui for tweaks in comments below.

Output:

192.168.0.1
192.168.4.5
#start_exceptions
192.168.88.88
192.168.76.6
#end_exceptions
192.168.5.55

p is a flag that says whether or not to print lines. It starts at the beginning as 1, so all lines are printed till I find a line starting with #start. Then I cat the contents of the exceptions file and stop printing lines till I find a line starting with #end, at which point I set the p flag back to 1 so remaining lines get printed.

If you want output to a file, add "> newfile" to the very end of the command like this:

awk '
    BEGIN       {p=1}
    /^#start/   {print;system("cat exceptions");p=0}
    /^#end/     {p=1}
    p' acl.txt > newfile

YET ANOTHER VERSION IF YOU REALLY WANT TO USE SED

If you really, really want to do it with sed, you can use nested address spaces, firstly to select the lines between #start_exceptions and #end_exceptions, then again to select the first line within that and also lines other than the #end_exceptions line:

sed '
   /^#start/,/^#end/{
      /^#start/{
         n
         r exceptions
      }
      /^#end/!d
   }
' acl.txt

Output:

192.168.0.1
192.168.4.5
#start_exceptions
192.168.88.88
192.168.76.6
#end_exceptions
192.168.5.55

ORIGINAL ANSWER

I think this will work:

sed -e '/^#end/r exceptions' -e '/^#start/,/^#end/d' acl.txt

When it finds /^#end/ it reads in the exceptions file. And it also deletes everything between /#start/ and /#end/.

I have left the matching slightly "loose" for clarity of expressing the technique.

Replacing text between two specific strings, You can use this command for replacing (assuming GNU sed as found on CentOS): sed -i "s/hspace='\([0-9]*\)'/style=\"margin: 0 \1px;\"/g" filename.html. The s is the substitute command of sed for find and replace; It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file named input.txt; Verify that file has been updated: more input.txt; Let us see syntax and usage in details. Syntax: sed find and replace text. The syntax is: sed 's/word1/word2/g' input.file


sed Find and Replace Text Between Two Strings or Words, For instance, the 1st indicator is a list of words pre { overflow:scr | The UNIX and Linux Forums. sed Find and Replace Text Between Two Strings or Words hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is � Replace text between two strings in file using linux bash. add "> newfile" to the very end of the command like this: based on Replace string with contents of


I had problem with Mark Setchell's solution in MINGW. The caret was not picking up the beginning of line. Indeed, is the detection of the separator dependent on it being at the beginning of the line? I came up with this awk alternative...

$ awk -v data="$(<exceptions)" '
BEGIN {p=1} 
/#start_exceptions/ {print; print data;p=0}
/#end_exceptions/ {p=1}
p
' acl.txt

Replacing text between two patterns, I would like to replace ], with ]]], between /* SECTION2-BEGIN */ and closing bracket to three closing brackets between 2 specified strings in a file: Tagged: pattern mataching and delete, pattern search and replace, shell, shell scripts, unix. Sed is a stream editor, meaning you can search and replace strings in files and use regex if needed. The changes can be done "in place" or save the modified content to a renamed file. This is especially good for replacing placeholder values in configuration files. There’s a million things to sed, see the man page.


How to replace text between two unique strings in file?, To replace the characters between @ and # with my/branch , use the following: $ sed -e 's!@[^@]*#!@my/branch#!' foo.txt -e� How to create tar.gz file in Linux using command line; Linux Check Memory Usage Using the CLI and GUI; Debian Linux 9.13 released and here is how to upgrade it; How to change permissions for a symbolic link under Linux/UNIX; How to check list of users in Unix; Display Linux or Unix logo with hardware Info using screenfetch / linux_logo


How to replace text between two patterns on different lines?, The s regex modifier causes perl to include newline characters in . With GNU sed, you can replace t; ba (branch out on successful A more manual approach would be to replace all newline character in the input file with NULLs, use a TeX - LaTeX � Software Engineering � Unix & Linux � Ask Different� When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [ [ command for pattern matching.


Replacing an uncertain amount of text between two strings. : bash, Replacing an uncertain amount of text between two strings. I need to Command to replace everything between STRING1 and STRING2 with STRING3 . I've been trying up even uglier. See man sed and man 7 regex (Linux) or man 7 re_format (BSD/Mac). Here is the simple script to download files from an AWS bucket: Tag: regex,linux,bash,grep. I want to search for the whole line below from my /etc/pam.d/su file to use it in a script: auth required pam_wheel.so use_uid there might be multiple spaces in between, and it might be commented out also, with multiple #'s. This is what I'm using : grep "^#*auth +required +pam_wheel\.so +use_uid$"