khal-el
khal-el

Reputation: 39

How to replace variable string with another variable string using sed

So I'm trying to replace a line content in a file with multiple lines but I can't get it to work. Here's the details:

Text File: /home/user1/file1.txt

File content:

The quick brown fox
The quick brown fox jumps over the lazy fat dog
Little brown and the lazy fat dog
Lazy dog and the fat lion

string1="The quick brown fox jumps over the lazy fat dog"

string2="The quick little black fox jumps over the lazy fat dog The quick big brown fox jumps over the lazy fat dog The lazy little brown fox jumps over the lazy fat dog"

Shell script file: /home/user1/replace_me.sh

So if I run the shell script file with the below sid command, the file content would be changed to:

The quick brown fox
The quick little black fox jumps over the lazy fat dog
The quick big brown fox jumps over the lazy fat dog
The lazy little brown fox jumps over the lazy fat dog 
Little brown and the lazy fat dog 
Lazy dog and the fat lion

I used How to replace a variable with another variable using sed and Replace a word with multiple lines using sed? as references:

These are the commands I tried with no luck :(

sed -i "s/$string1/$string2/" /home/user1/file1.txt

sed -i "/${string1}/{s/^.*/${string2}/" /home/user1/file1.txt

Below is the shell script file content:

#!/bin/bash

string1="The quick brown fox jumps over the lazy fat dog"

string2="The quick little black fox jumps over the lazy fat dog
The quick big brown fox jumps over the lazy fat dog
The lazy little brown fox jumps over the lazy fat dog"

sed -i "s/$string1/$string2/" /home/user1/file1.txt

#sed -i "/${string1}/{s/^.*/${string2}/" /home/user1/file1.txt

Upvotes: 0

Views: 2510

Answers (2)

potong
potong

Reputation: 58351

This might work for you (GNU sed):

echo "$string2" | sed "s/$string1/cat -/e" file

This uses the e flag in the substitution command to send $string2 to stdout using the stdin from the echoed pipe.

It should be noted $string1 must be a single line and not contain any meta characters. If $string1 does contains special characters, this might work in bash:

echo "$string2" |
sed 's/'"$(<<<"$string3" sed 's/[][^$.*&\/]/\\&/g')"'/cat -/e' file

This escapes any meta characters in $string1 which then becomes the LHS of the substitution regexp.

Upvotes: 1

KamilCuk
KamilCuk

Reputation: 140880

replace a line content in a file with multiple lines

In sed commands are delimetered with a newline. So when sed sees s/blabla/blabla<newline> is parses that part as a full command and exits with a missing closing /.

You can substitute each newline in the replacement string for two characters \n, which can be then substituted by sed for a newline once again.

string2=${string2//$'\n'/\\n}
sed "s/$string1/$string2/"

Note that sed parses first part of s command as a regex and in replacement string some strings (\1 & \L \U etc.) are also parsed specially.

This will only work if there are no newlines in string1. With GNU sed you can get away with newlines in string1 by using -z option, that will cause to parse the input as zero terminated strings.

Upvotes: 1

Related Questions