Solved

How To Append Text to Variable & SED

Posted on 2013-05-10
3
417 Views
Last Modified: 2013-05-10
I want to add ' in front and behind my variable and replace one character inside.

Here is my input: peter:chris
Here is my expected output: 'peter-chris'

I have tried, but my code is failing. My echo prints nothing. Code attempt attached.

Appreciate your help.

if [ -n "$string" ]
then
	set string = `echo "'"$string"'" | sed "s/:/-/"`
	echo $string;
fi

Open in new window

0
Comment
Question by:Neapolitan23
  • 2
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39157354
Which shell are you using, and how did you set $string before running that code?
0
 

Author Comment

by:Neapolitan23
ID: 39157368
bash shell.

string="$1" (value from the command line)
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39157416
In that case, I would have expected the output

peter:chris

and I don't understand why your echo prints nothing

If I change
   set string = `echo "'"$string"'" | sed "s/:/-/"`
to
   string=`echo "'"$string"'" | sed "s/:/-/"`
then I get
'peter-chris'

Although a simpler way to do that could be
echo "'${string/:/-}'"
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

831 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