views:

83

answers:

6

I have tried this countless ways, time to throw in the towel and ask the master...

#!/bin/bash

# Read in the VERSION.header file, this is a plain text file
# with returns, which in some of my tests, I am loosing the returns and getting
# one string wrapped only by the shell width
VERSION_HEADER=`cat VERSION.header`

sed 's/_VERSION_HEADER_/"$VERSION_HEADER"/g' DOCUMENTATION

The string '_VERSION_HEADER_' exists currently as line 1 in the file DOCUMENTATION. The file 'VERSION.header' has about 18 lines in it.

I am not attached to using sed. All I am looking to do is read in a file which acts as a template, store that files data as a variable, read in a second file, store it as a variable. Look in the second file for a marker, replace the marker with the data from the first file.

Example:

$cat VERSION.header

hi, this is my header file, how are you today
good, that is nice, great
have a nice day, k, thx, bye.

$cat DOCUMENTATION

_VERSION_HEADER_

# This is a documentation file, please read it
# If you do not read it, you will get stuck to the toilet.

And the result should be:

hi, this is my header file, how are you today
good, that is nice, great
have a nice day, k, thx, bye.

# This is a documentation file, please read it
# If you do not read it, you will get stuck to the toilet.

A: 
sed -i "s/_VERSION_HEADER_/$VERSION_HEADER/g" DOCUMENTATION

EDIT

Sorry, finally got your problem. You have a multi-line pattern. This may help. Also consider using Python instead:

#!/usr/bin/env python

f = open('doc.txt', 'r')
doc = f.read()
f.close()

f = open('VERSION.header', 'r')
versionHeader = f.read()
f.close()

doc = doc.replace('_VERSION_HEADER_', versionHeader)
print doc

This may be less efficient, though.

hudolejev
Thanks, trying to keep this in bash for now so it is portable to systems that will not always have python.Getting err: sed: 1: "DOCUMENTATION": extra characters at the end of D command
A: 

How about something like this:

#!/bin/bash

while read oneline; do
    VERSION_HEADER=${VERSION_HEADER}'\n'${oneline};
done < VERSION.header

sed 's/_VERSION_HEADER_/'"${VERSION_HEADER}"'/g' DOCUMENTATION
Amro
A: 

Give this a try:

sed -e '/_VERSION_HEADER_/{r VERSION.header' -e ';d}' DOCUMENTATION

That reads in the file when it finds the template string, then deletes the template string.

Dennis Williamson
I get an error: sed: 1: ";d}\n": extra characters at the end of d command
@allentown: It works for me using GNU sed 4.2.1. What version do you have (and what OS and version)?
Dennis Williamson
A: 

awk

awk 'FNR==NR{s=s" "$0"\n";next}/_VERSION_HEADER_/{$0=s}1' version.header documentation
ghostdog74
A: 

Maybe take a look at man 1 ed as well:

# http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed
[[ $(grep -c _VERSION_HEADER_ DOCUMENTATION) -eq 1 ]] && \
cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | ed -s DOCUMENTATION
   H
   /_VERSION_HEADER_/i
   .
   .c
   .
   .r VERSION.header
   wq
EOF
yabt
A: 

Using (FreeBSD) sed do something like:

VERSION_HEADER="$(sed -E -e 's/\\/\\\\/g' -e 's/&/\\\&/g' -e 's|/|\\/|g' -e 's/$/\\/g' VERSION.header)"

# note the space after ${VERSION_HEADER}
sed -E -i "" "/_VERSION_HEADER_/s/_VERSION_HEADER_/${VERSION_HEADER} /" DOCUMENTATION
cronisto