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.
views:
48answers:
3
+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
2010-04-13 07:35:39
+3
A:
cat file | while read line; do if [ "$line" = "#INSERT_HERE#" ]; then cat file_to_insert; else echo $line; fi; done
dogbane
2010-04-13 07:56:21
no need to pipe cat to while loop. also better to use `while read -r line`
ghostdog74
2010-04-13 08:13:58
also, you really have to quote the variable in `echo "$line"` or you risk destroying the formatting of the "outer" file.
glenn jackman
2010-04-13 14:14:46
+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
2010-04-14 08:23:25