Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SED: How to replace instances of String "{0}" with the value of a variable?

Posted on 2004-03-25
5
Medium Priority
?
579 Views
Last Modified: 2013-12-26
Hi,

Background:

In the following script, I call a SQL script which puts its resultset into a text file called quota_text.lis; which is referenced using the variable QUOTA_TEXT_LIS. I then take the first line of QUOTA_TEXT_LIS, remove any chunks of spaces using sed and tokenize it on a ':' delimter; placing each chunk of the line into a LINE_? variable.

COUNT=5

$ORACLE_HOME/bin/sqlplus $TELEAPPUSER/$TELEAPPPWD@$ORACLE_SID @$DBSCRIPTS/quota_text 1 "QUOTA"

TEXT_LINE=`head -n 1 $QUOTA_TEXT_LIS|sed -e 's/^[ \t]*//;s/[ \t]*$//' -e 's/ *: */:/g'`
echo "[$TEXT_LINE]"
LINE_1=`echo $TEXT_LINE | cut -f1 -d:`
LINE_2=`echo $TEXT_LINE | cut -f2 -d:`
echo "RXCOUNT is: $RXCOUNT"
LINE_3=`echo $TEXT_LINE | cut -f3 -d: | sed -e 's/{0}/$COUNT/g'`

The content of LINE_3 is:

"You have {0} unread messages"

and I want to replace the instance of {0} with the value of the COUNT variable using sed (or something else if there is a better solution???).

For some reason it currently results in LINE_3 looking like:

"You have $COUNT unread messages"

because the String "$COUNT" is replacing {0} rather the the value of the COUNT variable?

Question:

Can anyone tell me how to make it replace the {0} section with the actual assigned value of the COUNT variable, please?

Thanks
Scott
0
Comment
Question by:scurtis_1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10675870
Hi scurtis_1,
You must leave the single quote mode to use shell variables:

LINE_3=`echo $TEXT_LINE | cut -f3 -d: | sed -e 's/{0}/'$COUNT'/g'`


Cheers,

Stefan
0
 

Author Comment

by:scurtis_1
ID: 10675931
Stefan,

Thanks for the quick response. It seems to almost work; but now I get the following:

++ cut -f3 -d:
++ sed -e 's/{0}/' 0 /g
sed: -e expression #1, char 6: Unterminated `s' command

As you can see it has put the value of my variable into the sed command but now the sed command is failing?

Scott
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10675952
scurtis_1,

This is a classic one for PERL:

(bash array syntax:)

LINES= ( $( perl -e '
$text_line = <>;
$text_line =~ s/^\s+|\s+$//g;
$text_line =~ s/\s*\:\s*/\:/g;

@fields=split(/\s*\:\s*/,$text_line);
$fields[2] =~ s/\{0\}/$ENV{COUNT}/g;
print "\"$fields[0]\" "\"$fields[3]\" "\"$fields[2]\"";
'  $QUOTA_TEXT_LIS ) )

(ksh array syntax:)

set -A LINES $( perl -e '
$text_line = <>;
$text_line =~ s/^\s+|\s+$//g;
$text_line =~ s/\s*\:\s*/\:/g;

@fields=split(/\s*\:\s*/,$text_line);
$fields[2] =~ s/\{0\}/$ENV{COUNT}/g;
print "\"$fields[0]\" "\"$fields[3]\" "\"$fields[2]\"";
'  $QUOTA_TEXT_LIS )

Now you have ${LINES[0]}, ${LINES[1]}, ${LINES[2]} set with the values you need.

0
 
LVL 12

Accepted Solution

by:
stefan73 earned 1000 total points
ID: 10675964
scurtis_1,
> ++ sed -e 's/{0}/' 0 /g
> sed: -e expression #1, char 6: Unterminated `s' command

Looks like you inserted spaces into your expression, or $COUNT contains spaces. Try the double-quote mode instead:
LINE_3=`echo $TEXT_LINE | cut -f3 -d: | sed -e 's/{0}/'"$COUNT/g"`
0
 

Author Comment

by:scurtis_1
ID: 10676069
Excellent! That's got it!

Thanks

Scott
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question