Using sed to insert file content

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

Here is my code:

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

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

Any ideas ?

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/ {
         r $scriptPath/adapters/default/permissions.xml
     }" "$manifestFile"

How to insert the content of a file into another file before a pattern , sed '/<tag>/ r file2.txt' file1.txt And you are using the N command which doesn't print anything but reads next line, so at that In my case, it reads line with <tag> , then ends cycle, so prints the line and after it the content of the file and carry on� I know if you only use sed, you can use something like. sed -i '1 i\anything' file But can I do something like. head -1 file1 | sed -i '1 i\OutputFromPreviousCmd' file2 This way, I don't need to manually copy the output and change the sed command everytime. Update: Added the files I meant

Just remove i\\.


$ cat 1.txt

$ echo hello > 2.txt

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

$ cat 1.txt

Insert contents of a file after specific pattern match, Linux: Using sed to insert lines before or after a match In these examples, we will be dealing with a text file named “text.txt” that contains the� sed "i" command lets us insert lines in a file, based on the line number or regex provided. So, the lines will be added to the file AT the location where line number matches or BEFORE the line where pattern matches. sed with option -i will edit the file in place, i.e. unless you use the option -i, the changes will not be written to the file. (Explained in later section)

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.

Linux: Using sed to insert lines before or after a match – Fabian Lee , You can do it using sed insert with bash command substitiution sed "/<\/xa- datasource>/i $(<inputFile.txt)" file1.txt. this way text from inside inputFile.txt will be� Using Sed to insert file contents where pattern is matched. See more linked questions. Related. 813. Shell command to tar directory excluding certain files/folders. 1402.

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


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


$ cat test.txt
some stuff
some other stuff


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


some stuff
this is inserted file
this is inserted file
some other stuff

Adding content of a text file to middle of other text file before a , I don't think you can get the r command in sed to replace text in the middle of a line. You can use sed s to replace inserthere with the contents of� Find and replace text within a file using sed command. Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. 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.

CodeGnome's solution don't work, if the pattern is on the last line.. So I used 3 commands.

sed -i '/pattern/ i\
        ' $manifestFile
sed -i '/INSERTION_MARKER/r $scriptPath/adapters/default/permissions.xml' $manifestFile
sed -i 's/INSERTION_MARKER//' $manifestFile

How to insert the contents of one file to an exact place in another file, sed insert file at line number sed insert before match linux insert line into file sed insert replace sed replace line after match sed insert multiple lines after match This will insert file2 content before each matching line. To insert it only before the first matching line you could use a loop and just pull in the next line until you get to end of file: sed '/Pointer/{ r file2 N :l $!n $!bl } ${ /^$/!{ s/ $// } //d }' file1 <(printf %s\ )

using sed to insert file content into a file BEFORE a pattern, In our previous articles we learned sed with single commands Unix Sed Tutorial: Append, Insert, Replace, and Count File Lines How to insert content of file1 on the top of the file2, file3, file4, etc in same directory ? Link. You can also use regular expressions. For example to search all 3 digit numbers and replace them with the string number you would use: sed -i 's/\b[0-9]\{3\}\b/number/g' file.txt number Foo foo foo foo /bin/bash demo foobar number Another useful feature of sed is that you can use the ampersand character & which corresponds to the matched

Unix Sed Tutorial: Append, Insert, Replace, and Count File Lines, Any particular string in a text or a file can be searched, replaced and deleted by Combine sed with other commands; Insert an empty line in a file; Delete all� Linux: Using sed to insert lines before or after a match. The sed utility is a powerful utility for doing text transformations. In this article, I will provide an example of how to insert a line before and after a match using sed, which is a common task for customizing configuration files. I have also written a related article on setting and replacing values in a properties file using sed.

50 `sed` Command Examples – Linux Hint, It's not necessarily at the top or bottom of the file so I can't just use cat to cat the files together. I think probably sed with the r option is what I want�

  • The script has to be double quoted, otherwise $scriptPath won't be expanded. Could you please explain why is N needed at the end?
  • @hipe The r command only queues the read for insertion into the output stream. It doesn't actually read anything until the end of the cycle or when the next line is read. Without N the file wouldn't be inserted until after the pattern space was printed, making it function as an append rather than an insert. It's very non-intuitive.
  • Seems sed is very picky with the 'r' command. According to…: "Any additional characters before or after the filename are interpreted as part of the filename." I tried r someFile.txt # insert content of ... and it failed silently.
  • This works fine as long as pattern is not on the last line.
  • Hi, Todd and @vscharf, I found "h" & "g" are ommitable. I wrote this one-liner( ). Thanks !
  • It inserts after the pattern. How to insert the content before it ?
  • this doesn't work if the match is on the last line or if consecutive lines should match...
  • i found it most straightforward to use vim for this: vim "$fileToEdit" <<< ':$-1 r'"$fileToInsert"$'\n:wq'