LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-29-2007, 03:47 AM   #1
dsubbarao
LQ Newbie
 
Registered: Jan 2007
Distribution: Redhat
Posts: 14

Rep: Reputation: 0
changing the content of file in spec file


Hi All,

I want to modify some text in a file, say usrshare ,to a value of the variable $RPM_INSTALL_PREFIX in the %post section in the spec file.
I have written the following command:
sed -e 's/usrshare/$RPM_INSTALL_PREFIX/g' $RPM_INSTALL_PREFIX/GRE/abc.sh > $RPM_INSTALL_PREFIX/GRE/def.sh

But rather than replacing with the value of the variable $RPM_INSTALL_PREFIX it is just replaced with the plain $RPM_INSTALL_PREFIX string .

How to replace string with the value of $RPM_INSTALL_PREFIX??

Thanks in Advance
 
Old 01-29-2007, 04:37 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Try
Code:
sed -e 's/usrshare/'$RPM_INSTALL_PREFIX'/g' $RPM_INSTALL_PREFIX/GRE/abc.sh > $RPM_INSTALL_PREFIX/GRE/def.sh
Quotes force the shell to do variable expansion.
 
Old 01-29-2007, 07:40 AM   #3
dsubbarao
LQ Newbie
 
Registered: Jan 2007
Distribution: Redhat
Posts: 14

Original Poster
Rep: Reputation: 0
Hi colucix

It is giving me the error as SED: s unknown option

Thanks in Advance
 
Old 01-29-2007, 07:54 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
What shell are you using? As alternative you can use the eval command, as in
Code:
eval sed -e \'s/usrshare/$RPM_INSTALL_PREFIX/g\' $RPM_INSTALL_PREFIX/GRE/abc.sh > $RPM_INSTALL_PREFIX/GRE/def.sh
 
Old 01-29-2007, 10:50 PM   #5
dsubbarao
LQ Newbie
 
Registered: Jan 2007
Distribution: Redhat
Posts: 14

Original Poster
Rep: Reputation: 0
Hi colucix,

I am using bash shell. I have used eval command as :

eval sed -e \'s/usrshare/$RPM_INSTALL_PREFIX/g\' $RPM_INSTALL_PREFIX/GE/abc.sh > $RPM_INSTALL_PREFIX/GE/def.sh

It is giving the error:
sed: -e expression #1, char 13: unknown option to `s'


Thanks in Advance
 
Old 01-30-2007, 02:09 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
In this case something in the variable RPM_INSTALL_PREFIX is triggering this error and I'm pretty sure it is one or more / in the string! If this is the case, a workaround would be translate the / to something else, do the substitution with sed and then translate back. For example:

Code:
> cat test.txt  # this is my test file
usrshare ciao
ciao pippo
ciao usrshare

> export RPM_INSTALL_PREFIX=/usr/local
> eval sed -e \'s/usrshare/$RPM_INSTALL_PREFIX/g\' test.txt
sed: -e expression #1, char 13: unknown option to `s'

> export TR_INSTALL_PREFIX=`echo $RPM_INSTALL_PREFIX | tr "/" "@@@"`
> cat test.txt | sed -e s/usrshare/$TR_INSTALL_PREFIX/g| tr "@@@" "/"
/usr/local ciao
ciao pippo
ciao /usr/local
I suppose there is no "@@@" sequence of character in your install prefix, nor in the spec file. However I'm sure there is a better way to do this. Probably some sed guru will know!

Edit: of course the eval command is not needed anymore!

Last edited by colucix; 01-30-2007 at 02:52 AM.
 
Old 01-30-2007, 07:22 AM   #7
dsubbarao
LQ Newbie
 
Registered: Jan 2007
Distribution: Redhat
Posts: 14

Original Poster
Rep: Reputation: 0
Smile Hi

Hi colucix,

As u have expected the problem is with the forward slash.
But i have solved the problem by changing the separator from / to :
sed -e "s:usrshare:$RPM_INSTALL_PREFIX:g" $RPM_INSTALL_PREFIX/GE/abc.sh > $RPM_INSTALL_PREFIX/GE/def.sh

Thanks
 
Old 01-30-2007, 07:41 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Good! I didn't know about this possibility! Thanks!
 
  


Reply


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
How to extract spec file from rpm file nayankk Programming 18 11-12-2015 03:11 PM
Changing the content of already existing file superandrzej Slackware 3 02-02-2007 10:06 AM
shell script for changing a txt file content Flobsi Linux - Newbie 3 10-06-2006 04:10 AM
RPM Spec file creation: %file section question davidas Linux - Newbie 0 03-16-2004 11:36 PM
Patches for .spec file linuxdev Linux - Newbie 5 02-12-2004 02:32 PM


All times are GMT -5. The time now is 03:21 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration