Solved

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

Posted on 2004-03-25
5
567 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent! That's got it!

Thanks

Scott
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now