LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 12-11-2009, 04:37 PM   #1
matt007
LQ Newbie
 
Registered: Jun 2008
Posts: 14

Rep: Reputation: 0
purge a string for "whitespace and dashes"


So is there a command i can use to remove dashes ("-") and whitespaces from a string and get new string without the whitespace and dashes...
 
Old 12-11-2009, 04:38 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
In which language?
 
Old 12-11-2009, 04:46 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Sed, tr
 
Old 12-11-2009, 07:06 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
using bash shell, no need external commands
Code:
$ string="my string with spaces and - dashes - dashes"
$ echo ${string//[ -]/}
mystringwithspacesanddashesdashes
 
Old 12-11-2009, 07:28 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
The request was to remove whitespace - better to use a class rather than a single space character.
 
Old 12-11-2009, 07:33 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
sure thing, if that's what OP want, i refer him/her to bash reference: "3.5.8.1 Pattern Matching"
or just set it first
Code:
set -- $string
echo ${string//[ -]/}

Last edited by ghostdog74; 12-11-2009 at 07:42 PM.
 
Old 12-12-2009, 05:19 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
sure thing, if that's what OP want, i refer him/her to bash reference: "3.5.8.1 Pattern Matching"
or just set it first
Code:
set -- $string
echo ${string//[ -]/}
Ah! Bash pattern matching -- one of its most under-used features, perhaps because it is so un-intuitive and requires understanding filename expansion too.

But what does set -- $ string do that's useful in this case? AFAIK it just parses $string into $IFS-separated words and assigns them to $1, $2 ... ???

The requirement to remove whitespace and - can be met by including $IFS in the character range to be substituted
Code:
c:~$ x=$'a\t \n-b'
c:~$ echo "'${x//[$IFS-]}'"
'ab'
 
Old 12-12-2009, 06:16 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@catkin, what i meant is unsetting of IFS to become default...because by default IFS is whitespace, (ie space, tabs, newline). So setting it first using the default IFS make the string all become
Code:
$ s=$(printf "%s\t\t%s %s    %s" one two three four)
$ echo $s | od -c
0000000   o   n   e  \t  \t   t   w   o       t   h   r   e   e
0000020           f   o   u   r  \n
0000027

$ unset IFS
$ echo $s
one two three four
so the use of set -- $string is redundant

Last edited by ghostdog74; 12-12-2009 at 06:19 AM.
 
Old 12-12-2009, 07:24 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
@catkin, what i meant is unsetting of IFS to become default...because by default IFS is whitespace, (ie space, tabs, newline). So setting it first using the default IFS make the string all become
Code:
$ s=$(printf "%s\t\t%s %s    %s" one two three four)
$ echo $s | od -c
0000000   o   n   e  \t  \t   t   w   o       t   h   r   e   e
0000020           f   o   u   r  \n
0000027

$ unset IFS
$ echo $s
one two three four
so the use of set -- $string is redundant
Thanks ghostdog74 but I'm being dense and don't understand.

AIUI set -- $string does not unset IFS and unsettting IFS does not not set it to the default value; what it does do is make bash behave as if it had the default value.
Code:
c:~$ echo -n "$IFS" | od -c -to1
0000000      \t  \n
        040 011 012
0000003
c:~$ x=$'a\t \n-b'
c:~$ echo "'${x//[$IFS-]}'"
'ab'
c:~$ unset IFS
c:~$ echo "'${x//[$IFS-]}'"
'a
b'
 
Old 12-12-2009, 07:37 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by catkin View Post
Thanks ghostdog74 but I'm being dense and don't understand.
AIUI set -- $string does not unset IFS
first, yes you are right. secondly, in the last post, i said set -- $string is redundant. Maybe i chose the wrong words. I should have just said set -- $string is wrong.

Quote:
and unsetting IFS does not not set it to the default value;
well, maybe default value is the "wrong" phrase? anyway, i will refer you to the trusted bash ref (man bash) under special parameters
Quote:
Special Parameters
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a sin-
gle word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is
equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters
are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

so the supposedly correct version
Code:
string="my string with spaces and - dashes - dashes"
unset IFS
string=${string//-/}
set -- $string
for i in $@
do
    echo $i
done

Last edited by ghostdog74; 12-12-2009 at 07:50 AM.
 
Old 12-12-2009, 08:33 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
first, yes you are right. secondly, in the last post, i said set -- $string is redundant. Maybe i chose the wrong words. I should have just said set -- $string is wrong.


well, maybe default value is the "wrong" phrase? anyway, i will refer you to the trusted bash ref (man bash) under special parameters


so the supposedly correct version
Code:
string="my string with spaces and - dashes - dashes"
unset IFS
string=${string//-/}
set -- $string
for i in $@
do
    echo $i
done
OK -- I'm getting there. Thanks for bearing with me.

I tried that code and it printed the space-separated words of "my string with spaces and dashes dashes", one per line.

Using the information you showed from the man page, is this the technique you had in mind to do what matt007 asked for
Code:
c:~$ string="my string with spaces and - dashes - dashes"
c:~$ string=${string//-/}
c:~$ set -- $string
c:~$ IFS=''
c:~$ echo -n "$IFS" | od -c -to1  # For illustration, not required
0000000
c:~$ echo "$*"
my string with spaces and  dashes  dashes
Ugh! As I understand "If IFS is null, the parameters are joined without intervening separators" there should not have been any spaces in the echo "$*" output
 
Old 12-12-2009, 09:00 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i think i have confused you, sorry, this part
Code:
string="my<tabs tabs> string with spaces and - dashes - dashes"
unset IFS
string=${string//[ -]/}
is the alternative to solve OP's problem. the rest of the code, is not. they are there to confirm the spaces and dashes are removed. that's all.

Last edited by ghostdog74; 12-12-2009 at 09:38 AM.
 
Old 12-12-2009, 09:34 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
i think i have confused you, sorry,
Don't worry -- it's not hard!
Quote:
Originally Posted by ghostdog74 View Post
this part
Code:
string="my<tabs tabs> string with spaces and - dashes - dashes"
unset IFS
string=${string//-/}
is the alternative to solve OP's problem. the rest of the code, is not. they are there to confirm the spaces and dashes are removed. that's all.
Sorry -- it doesn't work for me and I still don't understand what effect unsetting IFS is intended to have
Code:
c:~$ string=$'my\t  string with spaces and - dashes - dashes'
c:~$ unset IFS
c:~$ string=${string//-/}
c:~$ echo $string
my string with spaces and dashes dashes
 
Old 12-12-2009, 09:40 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
well, as the ref says: "If IFS is unset, the parameters are separated by spaces".
I am just using this fact to change all the multiple tabs or multiple spaces that may be in the strings to just spaces...
then do the substitution...
Code:
string=${string//[ -]/}
I edited because i forget OP wants to remove spaces as well..
Of course, this is just an alternative. If you may like, there's also character class you can use
Code:
echo ${string//[[:space:]]/}
this is more easier...

Last edited by ghostdog74; 12-12-2009 at 09:43 AM.
 
Old 12-12-2009, 09:53 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
well, as the ref says: "If IFS is unset, the parameters are separated by spaces".
Wasn't that only for expanding "$*", not for any of the other special parameters and not for parameters in general and so not applicable to $string?
 
  


Reply



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
Perl - Can't use string ("html") as an ARRAY ref while "strict refs" OldGaf Programming 9 08-11-2009 11:14 AM
Set the status of "purge" packages - dpkg --get-selections kushalkoolwal Debian 7 01-09-2009 11:48 AM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
ignoring the "non-portable whitespace encountered at line " warning Jake13 Linux - Software 3 08-18-2004 12:34 PM

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

All times are GMT -5. The time now is 01:15 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