Replace pattern in file with another file? 
Author Message
 Replace pattern in file with another file?

I have a file A in which I have placed a tag (thisismytag):

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccccccccccccccccccccccccc
thisismytag
ddddddddddddddddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffff

I have a file B which I want to replace the tag (thisismytag).
Let's say file B looks like this:

GGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

Then I want the result to look like this:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccccccccccccccccccccccccc
GGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
ddddddddddddddddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffff

I'm pretty sure this is an easy task to do. I looked into
sed but couldn't quite find a solution.

Anyone have a solution?

Boerge



Mon, 27 Jan 2003 03:00:00 GMT  
 Replace pattern in file with another file?
i see that persian rugs are in fashion :)

------------------
#!/bin/sed -nf
/mytag/ {
 r myfile
 n

Quote:
}

p
-------------------

say you call the script s.sed and your 'include file' is called 'myfile'
given that you're on unix and s.sed is flagged executable, it should suffice
to execute as
cat origfile | ./s.sed > somefile

B.


Quote:
> I have a file A in which I have placed a tag (thisismytag):

> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
> ccccccccccccccccccccccccccccccccccccccccccccccc
> thisismytag
> ddddddddddddddddddddddddddddddddddddddddd
> eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Quote:
> ffffff

> I have a file B which I want to replace the tag (thisismytag).
> Let's say file B looks like this:

> GGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
> HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

> Then I want the result to look like this:

> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
> ccccccccccccccccccccccccccccccccccccccccccccccc
> GGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
> HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
> ddddddddddddddddddddddddddddddddddddddddd
> eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

- Show quoted text -

Quote:
> ffffff

> I'm pretty sure this is an easy task to do. I looked into
> sed but couldn't quite find a solution.

> Anyone have a solution?

> Boerge



Mon, 27 Jan 2003 03:00:00 GMT  
 Replace pattern in file with another file?
cuter script would be as follows in case you need to process the file beyond
the replacement
---------------
#!/bin/sed -f
/mytag/ {
 r myfile
 D
Quote:
}

---------------


Quote:
> I have a file A in which I have placed a tag (thisismytag):

> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
> ccccccccccccccccccccccccccccccccccccccccccccccc
> thisismytag
> ddddddddddddddddddddddddddddddddddddddddd
> eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Quote:
> ffffff

> I have a file B which I want to replace the tag (thisismytag).
> Let's say file B looks like this:

> GGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
> HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

> Then I want the result to look like this:

> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
> ccccccccccccccccccccccccccccccccccccccccccccccc
> GGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
> HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
> ddddddddddddddddddddddddddddddddddddddddd
> eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

- Show quoted text -

Quote:
> ffffff

> I'm pretty sure this is an easy task to do. I looked into
> sed but couldn't quite find a solution.

> Anyone have a solution?

> Boerge



Mon, 27 Jan 2003 03:00:00 GMT  
 Replace pattern in file with another file?


Quote:
>cuter script would be as follows in case you need to process the file
>beyond the replacement
>---------------
>#!/bin/sed -f
>/mytag/ {
> r myfile
> D
>}
>---------------

<snip>

Since this is an awk newsgroup, why not an awk script? Since I like to
overengineer, how about handling tags within lines rather than just
tags that span entire lines? And how about multiple tags in the same
line? And why not make the file a command line parameter? And why not
allow the filename to contain embedded whitespace?

match(s = $0, /mytag/) > 0 {
        do {
                printf("%s", substr(s, 1, RSTART - 1))
                system("cat \"" fname "\"")
                s = substr(s, RSTART + RLENGTH)
        } while (match(s, /mytag/) > 0)
        if ($0 !~ /^mytag$/) print s
        next

Quote:
}

{ print }

Put this in a file named foobar and run it as

awk -f foobar -v fname=tag_replacement_file  file_with_tags

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 28 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How do I replace a file using Write Characters to file.vi

2. How to replace or create a file using the open/create/replace.vi

3. problem: file dialog - replace file? - escape

4. file dialog - file pattern

5. Replacing a string from an input file within awk script

6. Help, how to replace string in multiple files

7. 2-file awk search-and-replace

8. in-file search/replace question without using mv or cp

9. Find/replace in the File Browser

10. replacing a line in a file

11. How to replace characters in a text file

12. Search and replace text in a file based on a specific line

 

 
Powered by phpBB® Forum Software