Solved

How To Append Text to Variable & SED

Posted on 2013-05-10
3
414 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
Comment Utility
Which shell are you using, and how did you set $string before running that code?
0
 

Author Comment

by:Neapolitan23
Comment Utility
bash shell.

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

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable 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…
The viewer will learn how to count occurrences of each item in an array.

762 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

7 Experts available now in Live!

Get 1:1 Help Now