1752402 Members
5651 Online
108788 Solutions
New Discussion юеВ

AWK/SED

 
opensource4all
New Member

AWK/SED

Here are certain conditions I need to fulfill

1)Replace a particular filename by the contents of the file in a master file

Tried it with sed but sed cudnt know how to replace the file contents something like
XYZ=srch pattern
ABC=FILE.
XYZ could be anywhere of course :)

sed 's/XYZ/`cat ABC`/g'
5 REPLIES 5
Peter Nikitka
Honored Contributor

Re: AWK/SED

Hi,

you where on the right path!
The first change for a solution would be to substitue the singlequotes to double quotes:

1) sed "s/XYZ/`cat ABC`/g"

BUT: you have to keep in mind, that
a character "/" in the file content of 'ABC' will make the substitute pattern invalid because "/" is the pattern delimiter. You can use any character NOT in file ABC for this purpose, e.g.

2) sed "s├В┬зXYZ├В┬з`cat ABC`├В┬зg"

There is no need for spawning an extra 'cat' process in posix shells, use

3) sed "s/XYZ/$(
Take extra care when ABC contains newlines.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: AWK/SED

My two cents using ex(1) though I prefer Peter's solution:

# ex -s +"/XYZ/r ABC | /XYZ/d | x" file
James R. Ferguson
Acclaimed Contributor

Re: AWK/SED

Hi:

# perl -pe 'if (s/XYZ//) {chomp;open(FH,"<","XYZ") or die;@a=;print @a}' ABC

Regards!

...JRF...
Frank de Vries
Respected Contributor

Re: AWK/SED

Try

export FILE=/path/nameofyourfile

sed "s!XYZ!$(<$FILE)!g"

When using " " quotes you can
embedd variables.
Look before you leap
Frank de Vries
Respected Contributor

Re: AWK/SED

How many points should I assign to replies?
Every response to your question is eligible to earn between 1-10 points. No need to worry about running out of points - when a truly awesome reply rolls in that deserves a 10, you will be able to assign it a 10! However, be careful to assign points based on the value that a reply truly provides. Use the following scale as a guideline:



o N/A: The answer was simply a point of clarification to my original question

o 1-3: The answer didn't really help answer my question, but thanks for your assistance!

o 4- 7: The answer helped with a portion of my question, but I still need some additional help!

o 8-10: The answer has solved my problem completely! Now I'm a happy camper!


Although assigning points is not mandatory, it is a key component of a strong, interactive community, and it is STRONGLY ENCOURAGED. Others have taken time to help you, so please take a moment to give them credit for their assistance!
Look before you leap