aioobe
aioobe

Reputation: 421090

Replace certain token with the content of a file (using a bash-script)

I have a file containing some text and the words INSERT_HERE1 and INSERT_HERE2. I'd like to replace these words with the content of file1.txt and file2.txt respectively.

I suspect sed or awk could pull it off but I've basically never used them.

Upvotes: 6

Views: 9049

Answers (7)

Nicholas Sushkin
Nicholas Sushkin

Reputation: 13810

I would use perl's in place replacement with -i.ext option

perl -pi.bak -e 's|INSERT_HERE1|`cat FILE1`|ge; 
                 s|INSERT_HERE2|`cat FILE2`|ge;' myfile

Then, use diff myfile.bak myfile to verify:

Upvotes: 1

dersimn
dersimn

Reputation: 875

This snippet replaces any section that is specified in the upper array. For e.g. here

<!--insert.txt-->

with the contents of "insert.txt"

#!/bin/bash

replace[1]=\<!--insert.txt--\>      ; file[1]=insert.txt
replace[2]=\<!--insert2.txt--\>     ; file[2]=insert2.txt

replacelength=${#replace[@]}

cat blank.txt > tmp.txt
for i in $(seq 1 ${replacelength})
do
    echo Replacing ${file[i]} ...
    sed -e "/${replace[i]}/r ${file[i]}" -e "/${replace[i]}/d" tmp.txt > tmp_2.txt
    mv tmp_2.txt tmp.txt
done
mv tmp.txt file.txt

If you're not afraid of .zip files you can try this example as long as it is online: http://ablage.stabentheiner.de/2013-04-16_contentreplace.zip

Upvotes: 1

MattoxBeckman
MattoxBeckman

Reputation: 3712

Sed does have a built-in read file command. The commands you want would look something like this:

$ sed -e '/INSERT_HERE1/ {
r FILE1
d }' -e '/INSERT_HERE2/ {
r FILE2
d }' < file

This would output

foo
this is file1
bar
this is file2
baz

The r command reads the file, and the d command deletes the line with the INSERT_HERE tags. You need to use the curly braces since sed commands and multi-line input since sed commands have to start on their own line, and depending on your shell, you may need \ at the end of the lines to avoid premature execution. If this is something you would use a lot, you can just put the command in a file and use sed -f to run it.

Upvotes: 8

SiegeX
SiegeX

Reputation: 140417

Easily done with Bash. If you need it to be POSIX shell let me know:

#!/bin/bash

IFS=  # Needed to prevent the shell from interpreting the newlines
f1=$(< /path/to/file1.txt)
f2=$(< /path/to/file2.txt)

while read line; do 
  if [[ "$line" == "INSERT_HERE1" ]]; then
     echo "$f1"
  elif [[ "$line" == "INSERT_HERE2" ]]; then
     echo "$f2"
  else
     echo "$line"
  fi
done < /path/to/input/file

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360325

This is suitable for small substitution files that may be substituted many times:

awk 'BEGIN {
        while ((getline line < ARGV[1]) > 0) {file1 = file1 nl line; nl = "\n"}; 
        close (ARGV[1]); nl = "";
        while ((getline line < ARGV[2]) > 0) {file2 = file2 nl line; nl = "\n"};
        close (ARGV[2]);
        ARGV[1] = ""; ARGV[2] = "" }
      { gsub("token1", file1); 
        gsub("token2", file2); 
        print }' file1.txt file2.txt mainfile.txt

You may want to add some extra newlines here and there, depending on how you want your output to look.

Upvotes: 1

codaddict
codaddict

Reputation: 455272

If you are okay with Perl you can do:

$ cat FILE1
this is file1

$ cat FILE2
this is file2

$ cat file
foo
INSERT_HERE1
bar
INSERT_HERE2
baz

$ perl -ne 's/^INSERT_HERE(\d+)\s+$/`cat FILE$1`/e;print' file
foo
this is file1
bar
this is file2
baz
$ 

Upvotes: 4

Jonathan
Jonathan

Reputation: 13624

This is not tested, but would be pretty close to what you need:

sed -e "s/INSERT_HERE1/`cat file1.txt`/" -e "s/INSERT_HERE2/`cat file2.txt`/" <file >file.out

It will not properly handle a file with slashes in it, though, so you may need to tweak it a bit.

I'd recommend Perl instead, though. Something like this:

#!/usr/bin/perl -w

my $f1 = `cat file1.txt`;
my $f2 = `cat file2.txt`;

while (<>) {
    chomp;
    s/INSERT_HERE1/$f1/;
    s/INSERT_HERE2/$f2/;
    print "$_\n";
}

This assumes that INSERT_HERE1 and INSERT_HERE2 may only appear once per line, and that the file1.txt does not include the text INSERT_HERE2 (wouldn't be difficult to fix, though). Use like this:

./script <file >file.out

Upvotes: 1

Related Questions