LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   purge a string for "whitespace and dashes" (https://www.linuxquestions.org/questions/programming-9/purge-a-string-for-whitespace-and-dashes-775041/)

matt007 12-11-2009 04:37 PM

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...

Komakino 12-11-2009 04:38 PM

In which language?

pixellany 12-11-2009 04:46 PM

Sed, tr

ghostdog74 12-11-2009 07:06 PM

using bash shell, no need external commands
Code:

$ string="my string with spaces and - dashes - dashes"
$ echo ${string//[ -]/}
mystringwithspacesanddashesdashes


syg00 12-11-2009 07:28 PM

The request was to remove whitespace - better to use a class rather than a single space character.

ghostdog74 12-11-2009 07:33 PM

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//[ -]/}


catkin 12-12-2009 05:19 AM

Quote:

Originally Posted by ghostdog74 (Post 3788425)
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'


ghostdog74 12-12-2009 06:16 AM

@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

catkin 12-12-2009 07:24 AM

Quote:

Originally Posted by ghostdog74 (Post 3788717)
@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'


ghostdog74 12-12-2009 07:37 AM

Quote:

Originally Posted by catkin (Post 3788760)
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


catkin 12-12-2009 08:33 AM

Quote:

Originally Posted by ghostdog74 (Post 3788764)
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 :(

ghostdog74 12-12-2009 09:00 AM

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.

catkin 12-12-2009 09:34 AM

Quote:

Originally Posted by ghostdog74 (Post 3788832)
i think i have confused you, sorry,

Don't worry -- it's not hard!
Quote:

Originally Posted by ghostdog74 (Post 3788832)
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


ghostdog74 12-12-2009 09:40 AM

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...

catkin 12-12-2009 09:53 AM

Quote:

Originally Posted by ghostdog74 (Post 3788865)
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?


All times are GMT -5. The time now is 08:30 AM.