Linux: How to replace all text between two lines and substitute it with the output of a variable using sed?
You can probably use this sed
:
sed -e '/BEGIN/,/END/ {//!d; /BEGIN/r file.html' -e '}' file
This will insert content of file.html
file between BEGIN
and END
. To save changes back to file:
sed -i -e '/BEGIN/,/END/ {//!d; /BEGIN/r file.html' -e '}' file
How to replace multiple lines between two patterns with content of a file using sed?
You can try this sed
sed -e '/begin/,/end/!b' -e '/end/!d;r file2.txt' -e 'd' file1.txt
Sed print each line of the file if option -n is not specified.
Before printing, sed execute the script given by all the -e option given.
The command b in the script tell to sed to end the script at this point.
So the first -e command tell sed to end the script and print the line for all lines not in begin and end.
The second -e command tell sed to print the file file2.txt when it find the line with end.
The third -e command tell sed to delete (not print) the lines from begin to end.
How to replace a whole line (between 2 words) using sed?
With your shown samples please try following. sed
doesn't support lazy matching in regex. With awk
's RS you could do the substitution with your shown samples only. You need to create variable val
which has new value in it. Then in awk
performing simple substitution operation will so the rest to get your expected output.
awk -v val="your_new_line_Value" -v RS="" '
{
sub(/text\.\n*[^\n]*\n*text/,"text.\n"val"\ntext")
}
1
' Input_file
Above code will print output on terminal, once you are Happy with results of above and want to save output into Input_file itself then try following code.
awk -v val="your_new_line_Value" -v RS="" '
{
sub(/text\.\n*[^\n]*\n*text/,"text.\n"val"\ntext")
}
1
' Input_file > temp && mv temp Input_file
How to swap text based on patterns at once with sed?
Maybe something like this:
sed 's/ab/~~/g; s/bc/ab/g; s/~~/bc/g'
Replace ~
with a character that you know won't be in the string.
How to replace a whole line with sed?
Try this:
sed "s/aaa=.*/aaa=xxx/g"
How do I replace lines between two patterns with a single line in sed?
This might work for you (GNU sed & bash):
sed $'/^four/{:a;N;/^seven/McNEWLINE\nba}' file
Find and replace text in a file between range of lines using sed
You can use sed addresses:
sed '19,33s/google/facebook/g' file
This will run the substitution on lines between and including 19 and 33.
The form of a sed command is as follows:
[address[,address]]function[arguments]
Where 19,33
is the addreses,s
ubstitute is function
and g
lobal is the argument
replace a unknown string between two known strings with sed
sed -i 's/WORD1.*WORD3/WORD1 foo WORD3/g' file.txt
or
sed -i 's/(WORD1).*(WORD3)/\1 foo \2/g' file.txt
You might need to escape round brackets, depends on your sed variant.
Replace several lines by one using sed
Assuming you don't really need to do this with sed, this will work using any awk in any shell on every UNIX box:
$ awk -F'[()]' '/^[^[:space:]]/{s=$2; next} {sub(/[^[:space:]]*\(/,"("s",")} 1' file
(A,B,condition_1)
(A,C,condition_2)
(B,A,condition_3)
(C,B,condition_1)
Related Topics
How to Show Line Number When Executing Bash Script
How to Find Out Mount/Partition a Directory or File Is On? (Linux Server)
Postgresql -Bash: Psql: Command Not Found
Do (Statically Linked) Dlls Use a Different Heap Than the Main Program
Get Final Url After Curl Is Redirected
Find All Storage Devices Attached to a Linux MAChine
Recursive Copy of a Specific File Type Maintaining the File Structure in Unix/Linux
When to Use Kernel Threads VS Workqueues in the Linux Kernel
Can You Prevent a Command from Going into the Bash Shell Command History
Linux Usb: Turning the Power on and Off
What Does a Typical ./Configure Do in Linux
Difference Between Linux Loadable and Built-In Modules
What Should Linux/Unix 'Make Install' Consist Of
What Occurs When a File Is 'Source'-D in Unix/Linux Context
How to Send Data to Local Clipboard from a Remote Ssh Session
Why Does Zookeeper Not Use My Log4J.Properties File Log Directory