6

Ubuntu 14.04

I want to insert few lines stored in a source file into a target file. I'll read all those lines in a variable $var i.e. if I do echo "${var}" then it'll give me the following output -- which I want to add in my apache config file at a given line.

My exact source file (aka ${var})contents are:

        ## apppush proxying - get content from live site --##
        RewriteRule /apppush/hreflang/(.*) http://localhost:8081/hreflang.php?u=https://www.company.com/apppush/hreflang/$1 [P,L]

        RewriteCond /www/retaildb/companycom/us/branches/somebranch/us/%{SCRIPT_FILENAME} -d [OR]
        RewriteCond /www/retaildb/companycom/us/branches/somebranch/us/%{SCRIPT_FILENAME} -f
        RewriteRule ^/apppush/retail/(.*) /www/retaildb/companycom/us/branches/somebranch/us/%{SCRIPT_FILENAME} [L]

        ## Search/GIGA apppush rewrite
        RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
        RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
        RewriteRule ^/apppush/(.*)/search/(.*) /www/giga/%{SCRIPT_FILENAME} [L]

        RewriteRule /apppush/(.*) https://www.company.com/apppush/$1 [P]
        ## END apppush Rewrites --##

To do the above operation using a simple example, I tried the following commands, which is giving me an error.

For ex: I want to add ${var} which contains some values (3 lines total) into giga.txt file at line #3.

$ cat giga.txt
1
2
3
4
5
$ var="$(echo -e "333-444-555\n444-555-666\n555-666-777")"
$ echo "${var}"
333-444-555
444-555-666
555-666-777
gigauser@re-gigadev-s03-dev-2-32-3-rno:~/SVN_WS/repos/shenziconf$
$ sed -i "3i${var}" giga.txt
sed: -e expression #1, char 18: unknown command: `-'

I was expecting the output in giga.txt file as:

1
2
333-444-555
444-555-666
555-666-777
3
4
5

The end goal is to insert source file contents at line #3 in giga.txt (Target file / apache config file).

There are other ways to do this using a program to pick line# 1st-to-N, add source file contents, then line#s N+1 -to- EndOfFile to get the desired output but I trying to see if sed or awk can do this --OR--

PS For ex: The following command can successfully do similar operation (i.e. insert lines taken from a source file at a given location of the target file) BUT I'm looking how to do it via using a $variable (if possible).

sed -i "3r sourcefile.txt" targetfile.txt

1 Answer 1

6

Since in the end you want to read from files and not a variable, you can use the r command directly: it reads a file and inserts it after the current line. So if you have giga.txt with

1
2
3
4
5

and source.txt with

333-444-555
444-555-666
555-666-777

you can use this command:

$ sed '2r source.txt' giga.txt            
1
2
333-444-555
444-555-666
555-666-777
3
4
5

Notice that I've changed the line number to 2, as r appends after the line, whereas i inserts before the line.


If you really want to insert from a variable, you have to prepare it a bit: i requires that newlines are escaped with \, so if you do this

var=$(sed '$!s/$/\\/' source.txt)

you'll have a variable that looks as follows:

$ echo "$var"
333-444-555\
444-555-666\
555-666-777

and you can use this sed command:

$ sed "3i$var" giga.txt 
1
2
333-444-555
444-555-666
555-666-777
3
4
5

If you have GNU sed, you can combine these two approaches (hat tip Sundeep's comment): GNU sed can read standard input from the special file /dev/stdin, so you can use

sed '2r /dev/stdin' giga.txt <<< "$var"

or, if your shell doesn't support here strings like Bash does,

echo "$var" | sed '2r /dev/stdin' giga.txt

where $var doesn't need to have its newlines escaped.

Sign up to request clarification or add additional context in comments.

15 Comments

Agree, I did find the solution but it'd be good if I can do the same using a variable where variable can be set directly from any other cmd output. Seems like r option is the way to go for now. OR I guess I have to create a random tmp file and use that with r while doing sed
@ArunSangal Do you want to modify the contents of the source file? Or why do you want to put it into a variable first?
@ArunSangal Updated with a solution that reads from a variable.
Yes, I took the example of file contents (as it was something similar to what I was trying to do) but I was planning to not read / create a file. It seems like using 3r srcfile vs 3i $var is what I'm comparing if the latter is possible
thanks a lot. Didn't know about r but now it's clear i vs r
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.