Solved

How To Append Text to Variable & SED

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

733 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