Insert Multiple Lines into a File After Specified Pattern Using Shell Script

Insert multiple lines into a file after specified pattern using shell script

Another sed,

sed '/cdef/r add.txt' input.txt

input.txt:

abcd
accd
cdef
line
web

add.txt:

line1
line2
line3
line4

Test:

sat:~# sed '/cdef/r add.txt' input.txt
abcd
accd
cdef
line1
line2
line3
line4
line
web

If you want to apply the changes in input.txt file. Then, use -i with sed.

sed -i '/cdef/r add.txt' input.txt

If you want to use a regex as an expression you have to use the -E tag with sed.

sed -E '/RegexPattern/r add.txt' input.txt

Shell script: Insert multiple lines into a file ONLY after a specified pattern appears for the FIRST time. (The pattern appears multiple times)

Use ed, not sed, to edit files:

printf "%s\n" "/ccc/r toinsert.txt" w | ed -s original.txt

It inserts the contents of the other file after the first line containing ccc, but unlike your sed version, only after the first.

Insert multiple lines in file before specified pattern

It's normal that you get that error, the syntax of your text is not compatibale with your sed command, allow me to elaborate:

  • First, you have a lot of /s in your text, and / is the delimiter insed, that confuses the command, which is why you get that error. So you should escape all / in the text you are using, by replacing them with \\/ (The extra \ is going to be interpreted by the shell).

  • Second, in the man for sed, we can see this small line about /i:

Insert text, which has each embedded newline preceded by a backslash

This means that you also need to add a \ before every newline, in your example this means adding \\ at the end of every echo.

Edit:

Thanks to Toby Speight's comment, I noticed that I forgot completly about the possiblity of changing sed's delimiter, which can make your life a lot easier, since you wouldn't have to add \\ before every / in your text. To do this all you need is to change this line sed -i "/4/i $CONTENT" $FILE to, for example, this sed -i "\\_4_i $CONTENT" $FILE.

Here's how your script will become once you introduce these changes:

#! /bin/sh
FILE=/tmp/sample.txt
form_xml_string()
{
echo "<number value=\"11942\">\\"
echo " <string-attribute>\"hello\"</string-attribute>\\"
echo "</number>"
}

create_file()
{
if [ -e $FILE ]
then
echo "Removing file $FILE"
rm $FILE
fi

i=1
while [ $i -le 5 ]
do
echo "$i" >> $FILE
i=$(( i+1 ))
done
}

create_file
cat $FILE

# file sample.txt has numbers 1 to 5 in each line
# Now append the content from form_xml_string to line before 4
# command I tried
CONTENT=`form_xml_string`
echo "CONTENT is $CONTENT"
sed -i "\\_4_i $CONTENT" $FILE
cat $FILE

Insert multiple lines of text before specific line using Bash

This should work:

sed -i '/Line to insert after/ i Line one to insert \
second new line to insert \
third new line to insert' file

Sed Insert Multiple Lines

For the new trouble :
Use double backslash \\

insert 4 spaces after adding multiple lines from a text file

This might work for you (GNU sed):

sed 's/^/    /' file1 | sed '/pattern/r /dev/stdin' file2

Pipe a sed amended file1 into a second invocation of sed matching pattern in file2.

The ameliorated file1 is presented as /dev/stdin and added to the second sed by way of the r command.

How to insert the content of a file two lines after the line where a pattern is found?

It looks a bit funny, but this works with both GNU sed and BSD sed (on Mac OS X), and should work with most versions of sed:

sed -e '/Unix/{N;N;p;r content' -e 'd;}' data

Or:

sed -e '/Unix/{
N
N
p
r content
d
}' data

The N commands add extra lines to the pattern space (so the pattern space holds three lines containing Unix, Windows and Database); the p command prints the pattern space; the r content reads the file content and adds it to the output; the d deletes the pattern space; the {} group these operations so that they only occur when the input line matches Unix.

The r content must be at the end of a line of the script, or at the end of a -e argument, as shown. Trying to add a semicolon after it does not work (after all, the file name might contain a semicolon).



Related Topics



Leave a reply



Submit