Sed: Inserting file in another file 
Author Message
 Sed: Inserting file in another file

I'm trying to insert a file (A) into another file (B) by replacing a
specific
pattern in B with the complete A-file.
I'm writing a bourne shell-script, and I use SED in this script to try to
achieve this.
==================
The following way seems to work:

#!/bin/sh
cat origfile | sed -f sedfile > result

Where sedfile is:
/tag_to_replace/ {
r lines_to_insert
D

Quote:
}

==================
However, in my script "lines_to_insert" is a variable storing the
name of the file having the lines to be inserted. Therefore
I want to write it all in the bourne-shell script without using
"-f" option for the sed-data (unless I find a way to pass the
parameter to the datafile (-f sedfile)).
I have tried something like this:

cat B | sed '/tag_to_replace/ {
r $lines_to_insert
D

Quote:
}' > $merged_file

What happens is that I get the B-file with the tag deleted but
no code inserted.
Does anyone have a clue as to what I'm doing wrong?
--
Boerge Haga
PS: Remove "NOSPAM" and ".invalid" to reply by mail.


Tue, 11 Mar 2003 03:00:00 GMT  
 Sed: Inserting file in another file

Quote:

>I'm trying to insert a file (A) into another file (B) by replacing a
>specific pattern in B with the complete A-file.  I'm writing a bourne
>shell-script, and I use SED in this script to try to achieve this.
>==================
>The following way seems to work:

>#!/bin/sh
>cat origfile | sed -f sedfile > result

>Where sedfile is:
>/tag_to_replace/ {
>r lines_to_insert
>D
>}
>==================
>However, in my script "lines_to_insert" is a variable storing the name
>of the file having the lines to be inserted. Therefore I want to write
>it all in the bourne-shell script without using "-f" option for the
>sed-data (unless I find a way to pass the parameter to the datafile (-f
>sedfile)).  I have tried something like this:

>cat B | sed '/tag_to_replace/ {
>r $lines_to_insert
>D
>}' > $merged_file

>What happens is that I get the B-file with the tag deleted but no code
>inserted.  Does anyone have a clue as to what I'm doing wrong?

You mean, besides:
        1) Posting to the wrong newsgroup - what you have is an underlying
           sed problem, made visible by a shell quoting problem.  This is
           neither a sed nor a shell newsgroup.
        2) Using the wrong tool for the job - this would be trivial in AWK,
           though, on odd numbered Thursdays, I'd be tempted to do it as a
           command line 'ex' script.
        3) Failure to specify platform (although in this case, the use of
           cat and sed tells us that it is probably generic Unix)
        4) UUOC

Anyway, the answer to your question is "Use double quotes, instead of
single quotes"



Tue, 11 Mar 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. sed: input file = output file

2. Newbie: inserting an XML-file in an XML-file

3. help on sed file

4. log file processing - awk or sed

5. Find the first occurence of file in pwd with sed

6. Using awk And/Or sed To Edit Fourth Field Of File

7. how to insert a comma using either sed or awk

8. sed - extracting "*.cpp" from a file

9. SED multiple file redirection?

10. How to get info from a file with sed/awk/perl

11. deleting records in a file using AWK, SED, or kshell

12. Getting awk (or sed or anything else) to put single quotes into file

 

 
Powered by phpBB® Forum Software