LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2011, 08:08 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
Strip spaces outside the string.


Hi, I want to strip spaces outside the string but not within the string, eg.
" Hi How r u "
becomes
"Hi How r u"
and
"hi u r good"
is unchanged.
Thanks,
Ted
 
Old 11-11-2011, 09:08 AM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
Hi!
You haven't said in what language you are programming or what kind of shell... anyway the function you're looking for is called "trim" in most programming languages, look for such a function in your documentation.
 
Old 11-11-2011, 09:09 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Have a look at this:
Code:
echo "Hi How r u " | sed -r 's/(^  *|  *$)//'
Hope this helps.

@clvic: Good point Mine is based on shell scripting using sed in this case.

Last edited by druuna; 11-11-2011 at 09:10 AM.
 
Old 11-11-2011, 09:15 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,479

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
If your strings are in a file you can use the streaming editor, sed:
Code:
sed 's/"  */"/;s/  *"/"/' file
That's s/"space-space* and s/space-space*".

Using your example,
Code:
prompt: echo \" Hi How r u \" | sed 's/"  */"/;s/  *"/"/'
"Hi How r u"
prompt:
If you do this against a file (as above), you would redirect the output into another file or wherever you'd like to send it.

Hope this helps some.
 
Old 11-11-2011, 09:24 AM   #5
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
This works too:

Code:
#!/bin/bash
string=" Hi How r u "
string=$(echo $string)
edit
see pitfalls with this approach below

Last edited by Juako; 11-11-2011 at 04:21 PM.
 
Old 11-11-2011, 10:03 AM   #6
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Original Poster
Rep: Reputation: 2
Hi, Thanks for the replies, I dont know why I cant get any of them to work:
Code:
FILENAME=$(echo $FILENAME | sed -r 's/(^  *|  *$)//')
echo "<a><font color=\"black\">$FILENAME</font></a>"
Some of the FILENAME strings are like this:
Code:
"  12.05.08.rtf                      "
"  app_online_en.pdf                 "
"  Course Enrollment Form.pdf        "
"  CyberLink   "
(Without the quotations)

Thanks,
Ted

Last edited by ted_chou12; 11-11-2011 at 10:06 AM.
 
Old 11-11-2011, 10:09 AM   #7
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
It works here, strange...

Code:
#!/bin/bash
filename="  12.05.08.rtf                "
echo "<a><font color=\"black\">$(echo $filename)</font></a>"

Last edited by Juako; 11-11-2011 at 10:10 AM.
 
Old 11-11-2011, 10:19 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@ted_chou12:

Code:
"  12.05.08.rtf                      "
"  app_online_en.pdf                 "
"  Course Enrollment Form.pdf        "
"  CyberLink   "
Is the above (without the double quotes) the content of a string or a file?

What is the output of
Code:
echo $FILENAME
 
Old 11-11-2011, 12:00 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
I am with druuna as these are some very strange looking file names to have ... ie the user placing so many spaces before and after a file name on creation is a little bizarre.
 
Old 11-11-2011, 04:14 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

It appears to me like the names have been stored in a text file with column formatting of some kind. It may help if you could explain in detail what the situation is.

I think we should be careful about possible strings of whitespace inside the names too. Any solution like Juako's that relies on shell word-splitting should be suspect.

Here's a safe way to do it using bash's regex ability.

Code:
re='^[[:space:]]*(.*[^[:space:]])[[:space:]]*$'

[[ "$FILENAME" =~ $re ]]
echo "<a><font color=\"black\">${BASH_REMATCH[1]}</font></a>"
 
1 members found this post helpful.
  


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
How to remove spaces from a string pawan_songara Programming 14 08-30-2006 10:20 PM
strip number from string (c code) alaios Programming 8 09-26-2005 01:47 AM
Bash - Strip Spaces from string then cat cmfarley19 Programming 8 07-25-2004 01:01 PM
Removing spaces from string qcoder Programming 3 07-05-2004 01:35 PM
spaces in a string Longinus Programming 18 03-08-2004 07:02 PM


All times are GMT -5. The time now is 09:36 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration