LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2004, 09:32 AM   #1
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
Bash - Strip Spaces from string then cat


I know this has to be such a simple solution, but I can not figure it out...
I have a string
Code:
GALNAME="Our Family Photos"
I want to create a new string with teh same name except replacing spaces with underscores. I do that with
Code:
GALNAME_NO_SP=`echo "$GALNAME" | tr " " "_"`
That wroks fine. Here's where I'm stuck. I want to create a third string That concatenates the new string with some other info and strings.
Code:
PAGENUMER=1
echo "$GALNAME_NO_SP_page$PAGENUMBER"
I want the to echo "Our_Family_Photos_page1". All it echos is "1". The shell is interpreting "$GALNAME_NO_SP_page" as a variable.
How can I get the shell to interpret $GALNAME_NO_SP as the variable and cat "_page" at the end? If I do
Code:
echo "$GALNAME_NO_SP _page$PAGENUMBER"
then I get "Our_Family_Photos _page1" with a space

Any thoughts?
 
Old 07-25-2004, 09:37 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
i'd just say do things in a different order:
Code:
echo $GALNAME page$PAGENUMBER | tr ' ' '_'
 
Old 07-25-2004, 09:43 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
oh you could also quote the vairable.... "$GALNAME"_page... but that should be just for reference now i hope!
 
Old 07-25-2004, 10:06 AM   #4
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
Both are excellent appraches.
It was early here on the US east coast.
Brain was still warming up.

Thanks.
 
Old 07-25-2004, 10:09 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,786

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
or use this syntax:
${GALNAME_NO_SP}_page$PAGENUMBER"
 
Old 07-25-2004, 10:10 AM   #6
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
Acid_kewpie,
That did it.

Cheers.

Last edited by cmfarley19; 07-25-2004 at 10:12 AM.
 
Old 07-25-2004, 10:12 AM   #7
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
jlliagre,
That will do it too.

Thanks.
 
Old 07-25-2004, 10:15 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Or:
Code:
echo "${GALNAME_NO_SP}_page$PAGENUMBER"
As "man bash" puts it:
Quote:
${parameter}
The value of parameter is substituted. The braces are required
when parameter is a positional parameter with more than one
digit, or when parameter is followed by a character which is not
to be interpreted as part of its name.
<edit>
Hmm, seems jlliagre types faster than me :-)
</edit>

Last edited by Hko; 07-25-2004 at 10:16 AM.
 
Old 07-25-2004, 12:01 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,786

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Well, my answer was less detailed ...
I think it's a draw
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strip number from string (c code) alaios Programming 8 09-26-2005 12:47 AM
problems in removing white spaces from string of text monil Programming 7 03-08-2005 11:28 AM
Removing spaces from string qcoder Programming 3 07-05-2004 12:35 PM
spaces in a string Longinus Programming 18 03-08-2004 06:02 PM
need to innitialize string variable with multiple spaces clsonnt Programming 3 08-11-2003 10:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration