Solved

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

Posted on 2004-03-25
5
570 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 107
Separate files macro - Errors on larger data set 18 65
sumNumber challenge 16 112
Detect file exist or not 3 134
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

26 Experts available now in Live!

Get 1:1 Help Now