LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-05-2008, 09:38 AM   #1
BigRedBall
LQ Newbie
 
Registered: Feb 2008
Posts: 7

Rep: Reputation: 0
Text replacement question: sed/awk/perl whatever


Hi all,
Apologies if this is really obvious or it's been answered before (I can't find it in the wiki or the forums).


I need a way to replace a piece of text in a file1 with the contents of file2. In the file1, I already have a piece of 'placeholder' text, which is where the the contents of file2 are to be inserted.

I've seen various things like this:

Code:
 perl -pi -e 's/find/replace/g'
and

Code:
sed -e 's/find/replace/' file

But what I can't figure out is how to get 'replace' to be the contents of a file, rather than a given string.

<superfluous_detail>
Specifically, what I'm trying to do is insert the base64 of file2 into an XML document (file1) which was generated using XSLT. But I coudn't find a way to do a base64 of a file in XSLT so I've used XSLT to insert a bit of placeholder text, and I hope to find a way of shell scripting the operation of inserting the base64 of file2 into the placeholder's position.

In the question, I'm assuming that file2 already contains the base64 of the file I want by using the shell command for that, but if the solution could do the base64 operation inline on the original file that'd be super-cool. Not having to rely on another command would be very handy.
</superfluous_detail>

I'll be running any solution on Red Hat Enterprise Linux 3 and/or 5.

Thanks in advance for any ideas.

Last edited by BigRedBall; 02-05-2008 at 09:55 AM.
 
Old 02-05-2008, 09:50 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,564
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
The elaborate way could be to 'SOMEVAR=`cat file2`; 'sed -i "s|SOMETERM|${SOMEVAR}|g" file1' but 'sed -i "s|SOMETERM|$(cat file2)|g" file1' works too. This means 'sed -i "s|SOMETERM|$(mimencode -b file2|xargs)|g" file1' should work too.
 
Old 02-05-2008, 10:24 AM   #3
BigRedBall
LQ Newbie
 
Registered: Feb 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Hi unSpawn,
Thanks for the idea, and for the quick reply!

I think it's pretty close, but I get this error when running it:
Code:
sed: 1: "s|SOMETERM|< ...": unescaped newline inside substitute pattern
My full shell script looks like this:

Code:
#!/usr/bin/env bash
SOMEVAR=`cat file2`

sed -i .bak "s|SOMETERM|${SOMEVAR}|g" test.xml
I've left the base64 bit out for clarity. I think it's because the file contents contains newlines etc.

Thanks.
 
Old 02-05-2008, 10:33 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,564
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
Quote:
Originally Posted by BigRedBall View Post
I think it's because the file contents contains newlines etc.
That's why I piped 'mimencode' output through 'xargs'.
 
Old 02-05-2008, 11:50 AM   #5
BigRedBall
LQ Newbie
 
Registered: Feb 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks,
I'll give that a go.
 
Old 02-05-2008, 11:58 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
You could replace a ^M character with \n in your $SOMEVAR replace pattern. You might want to use a different character than '*' if it might be in the replacement pattern file.
var=$(cat file2 | tr '\n' '*' | sed 's/*/\\n/g')
 
Old 02-05-2008, 12:53 PM   #7
BigRedBall
LQ Newbie
 
Registered: Feb 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Hi all,
I discovered that the machine in question is so old that it doesn't have mimeencode or similar. So I've had to use a Perl command instead.

I also decided to split the process up so that it doesn't do an replace on the input file, so now it uses stdout to redirect the replaced version. Here's the code I've come up with - it seems to work.

'placeholder' is the placeholder text.
$infile is the file we want to base64
$container is the file we want to insert the base64 version into.

$container-final.xml will contain the final output.

Code:
sed -e "s|placeholder|$(perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' < $infile | xargs)|g" $container > $container-final.xml
Thanks for all your help folks. Much appreciated.
 
  


Reply

Tags
perl, replacement, script, scripting, sed, shell, text


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Grep,Awk,Sed]Parsing text between XML tags. ////// Programming 5 07-26-2011 12:54 PM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 10:00 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 01:34 AM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 10:07 PM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 09:24 PM


All times are GMT -5. The time now is 06:25 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration