Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1836
  • Last Modified:

sed with bash variable

Dear all.

A little bit stuck here using sed in a bash script.

I want to change a single line in a file, e.g
---------
...
PREFIX=/usr/lib
...
---------

to become
---------
...
PREFIX=/usr/X11R6
...
---------

I can do that from command line

$ sed -e `s/^PREFIX=.*/PREFIX=\/usr\/X11R6`  the_file

But I got difficulty to do that in a bash script,
when the substituter is a variable.

#/bin/sh
PREFIX=/usr/X11R6
THEFILE=the_file

# How to call sed to change PREFIX=anything to PREFIX=$PREFIX here ?

# these did not work
sed -e `s/PREFIX=.*/$PREFIX` $THEFILE
sed -e "s/PREFIX=.*/$PREFIX" $THEFILE


Thanks
0
Kocil
Asked:
Kocil
  • 9
  • 5
  • 2
  • +2
3 Solutions
 
shivsaCommented:
cat $i | sed 's/'$pattern'/'$replacement'/g' > $j
check this post.
http://www.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20239657.html
0
 
shivsaCommented:
or try this

PREFIX = `echo $PREFIX |sed "s/lib/X11R6/"`
0
 
KocilAuthor Commented:
>> cat $i | sed 's/'$pattern'/'$replacement'/g' > $j

It does not work, error:
sed: -e expression #1, char 12: Unknown option to `s'



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
KocilAuthor Commented:
Uh ... it works for:

PREFIX=anything
cat the_file | sed 's/PREFIX=.*/'$PREFIX'/g' > $the_file

does not works for:
PREFIX=/usr/lib
cat the_file | sed 's/PREFIX=.*/'$PREFIX'/g' > $the_file

hmmm ???
I guess the '/' is the problem. How to fix that ?
0
 
shivsaCommented:
PREFIX=`echo $PREFIX |sed "s/lib/X11R6/"`

this is working perfectly on my system.
0
 
KocilAuthor Commented:
I meant
cat $the_file | sed 's/PREFIX=.*/'$PREFIX'/g' > $the_file
0
 
KocilAuthor Commented:
> PREFIX=`echo $PREFIX |sed "s/lib/X11R6/"`

Yeah. If the replacement does not contain /, it works.
Unfortunatelly, my case contains / most of the time


0
 
shivsaCommented:
try putting " " around the variable.
cat $the_file | sed 's/PREFIX=.*/"$PREFIX"/g' > $the_file
0
 
KocilAuthor Commented:
I tried ...

$ PREFIX=/usr/lib

$ echo $PREFIX
/usr/lib

$ echo "PREFIX=xxx" | sed 's/PREFIX=.*/PREFIX="$PREFIX"/g'
PREFIX="$PREFIX"

still not working :)

0
 
glassdCommented:
So you need to change one line in a file. Sed will not write back to the source file so you need to use a temp file:

From="/usr/lib"
To="/usr/X11R6"

sed -e ":^PREFIX=:s:$From:$To:" infile > tempfile
cp tempfile infile
rm tempfile

Using the colon as the field separator in sed avoids all the problems caused by the slash contained in the variable.
0
 
shivsaCommented:
u can use your own string into this and work it out.

PREFIX=`echo $PREFIX |sed "s/\/usr\/lib/\/usr\/X11R6/"`
echo $PREFIX
/usr/X11R6
0
 
KocilAuthor Commented:
>> sed -e ":^PREFIX=:s:$From:$To:" infile > tempfile

I don't understand the syntax. I copied it as is, but it did not work on an infile containing

AAAA
PREFIX=/usr/lib
BBBB

0
 
KocilAuthor Commented:
>> PREFIX=`echo $PREFIX |sed "s/\/usr\/lib/\/usr\/X11R6/"`

Yes, so the problem is I have to convert "/usr/X11R6" to "\/usr\/X11R6".

ah ... I got it

PREFIXED=`echo $PREFIX | sed -e 's/[\/]/\\///g'`
cat $the_file | sed -e 's/^PREFIX=.*/PREFIX='$PREFIXED'/g' > $the_file

It solved now,
But I'm interested on glassd's slash-trouble free solution and waiting for his responds.







0
 
glassdCommented:
Sorry, untested code. Dodgy syntax. This should work:

Assuming the input file:

   AAAA
   PREFIX=/usr/lib
   Prefix=/usr/lib
   BBBB

Then running the script:

   From="/usr/lib"
   To="/usr/X11R6"
   sed -e "/^PREFIX=/s:$From:$To:" infile

should return:

   AAAA
   PREFIX=/usr/X11R6
   Prefix=/usr/lib
   BBBB

The first part (/^PREFIX=/) finds all lines beginning with "PREFIX=" (the "^" character represents the beginning of the line). This pattern must be enclosed in "/" characters (my mistake there).

Then you can use whatever character you like as the separator. A colon is common as it does not normally appear in file names.

The rest of the line substitutes any occurance of "/usr/lib" (held in the variable $From) with "/usr/X11R6" (held in the variable $To).

So the sed command should change "/usr/lib" to "/usr/X11R6" on any line starting "PREFIX=".

Does all that make sense? Not too sure!
0
 
sunnycoderCommented:
abc=/a/b/c
sed -e "s:^PREFIX=.*:PREFIX=$abc:" test.txt

worked for me
0
 
ahoffmannCommented:
doh, to much comments to read carefully ..
sound like / problem, see glassd and sunnycoder's suggestions to

 cat $the_file | sed 's#PREFIX=.*#'$PREFIX'#g' > $the_file
0
 
KocilAuthor Commented:
>> sed -e "s:^PREFIX=.*:PREFIX=$abc:" test.txt
EXCELLENT

>> cat $the_file | sed 's#PREFIX=.*#'$PREFIX'#g' > $the_file
works too

>> sed -e "/^PREFIX=/s:$From:$To:" infile
works too

I split the point.
Thanks all.
0
 
KocilAuthor Commented:
The lesson I learnt .... we can use : and # as a separator.
They should document it in an easier place :)
0
 
ahoffmannCommented:
> The lesson I learnt .... we can use ..
.. any (printable) character as separator ;-)
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 9
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now