Solved

How To Append Text to Variable & SED

Posted on 2013-05-10
3
416 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

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 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