Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed to insert file content

Tags:

bash

sed

I'm trying to insert a file content before a given pattern

Here is my code:

sed -i "" "/pattern/ {
i\\ 
r $scriptPath/adapters/default/permissions.xml"
}" "$manifestFile"

It adds the path instead of the content of the file.

Any ideas ?

like image 969
ridan Avatar asked Jun 28 '12 10:06

ridan


People also ask

How do I add the contents of a file in Linux?

You need to use the >> to append text to end of file. It is also useful to redirect and append/add line to end of file on Linux or Unix-like system.

How do I add content to a file in Terminal?

It's possible to add few lines of text in a file, without ever opening a text editor. Open your terminal and create a new file 'myfile' with the touch-command. Now you can check, if your new file is empty. With the cat-command you can print the content of your text files.

How do I append with sed?

We've learned that sed's “a” and “i” commands can insert or append a new line. The backslash character after the “a” or “i” command doesn't function as the part of an escape sequence, such as \t as a tab or \n as a newline. Instead, it indicates the beginning of the text in the new line we're inserting.

How do you insert a file in Unix?

To enter text, you must be in the insert mode for which simply type i. To come out of the insert mode, press the Esc key, which will take you back to the command mode. Hint − If you are not sure which mode you are in, press the Esc key twice; this will take you to the command mode. You open a file using the vi editor.


4 Answers

I tried Todd's answer and it works great,

but I found "h" & "g" commands are ommitable.

Thanks to this faq (found from @vscharf's comments), Todd's answer can be this one liner.

sed -i -e "/pattern/ {r $file" -e 'N}' $manifestFile

Edit: If you need here-doc version, please check this.

like image 151
benok Avatar answered Oct 22 '22 20:10

benok


In order to insert text before a pattern, you need to swap the pattern space into the hold space before reading in the file. For example:

sed "/pattern/ {
         h
         r $scriptPath/adapters/default/permissions.xml
         g
         N
     }" "$manifestFile"
like image 20
Todd A. Jacobs Avatar answered Oct 22 '22 21:10

Todd A. Jacobs


Just remove i\\.

Example:

$ cat 1.txt
abc
pattern
def

$ echo hello > 2.txt

$ sed -i '/pattern/r 2.txt' 1.txt

$ cat 1.txt
abc
pattern
hello
def
like image 35
Igor Chubin Avatar answered Oct 22 '22 22:10

Igor Chubin


I got something like this using awk. Looks ugly but did the trick in my test:

command:

cat test.txt | awk '
/pattern/ {
    line = $0;
    while ((getline < "insert.txt") > 0) {print};
    print line;
    next
}
{print}'

test.txt:

$ cat test.txt
some stuff
pattern
some other stuff

insert.txt:

$ cat insert.txt
this is inserted file
this is inserted file

output:

some stuff
this is inserted file
this is inserted file
pattern
some other stuff
like image 22
bcelary Avatar answered Oct 22 '22 21:10

bcelary