Solved

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

Posted on 2004-03-25
5
575 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
  • 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 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 96
deburging in oracle form 12 105
sum28 challenge 31 115
noX challenge 17 130
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 …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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