tags:

views:

48

answers:

3

Say I have file A, in middle of which have a tag string "#INSERT_HERE#". I want to put the whole content of file B to that position of file A. I tried using pipe to concatenate those contents, but I wonder if there is more advanced one-line script to handle it.

+4  A: 
$ cat  file
one
two
#INSERT_HERE#
three
four

$ cat  file_to_insert
foo bar
bar foo

$ awk '/#INSERT_HERE#/{while((getline line<"file_to_insert")>0){ print line };next }1 ' file
one
two
foo bar
bar foo
three
four
ghostdog74
+3  A: 
cat file | while read line; do if [ "$line" = "#INSERT_HERE#" ]; then cat file_to_insert; else echo $line; fi; done
dogbane
no need to pipe cat to while loop. also better to use `while read -r line`
ghostdog74
also, you really have to quote the variable in `echo "$line"` or you risk destroying the formatting of the "outer" file.
glenn jackman
+1  A: 

Use sed's r command:

$ cat foo
one
two
#INSERT_HERE#
three
four
$ cat bar
foo bar
bar foo
$ sed '/#INSERT_HERE#/{ r bar
> d
> }' foo
one
two
foo bar
bar foo
three
four
William Pursell
Wow, this looks great!
solotim