ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
@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.
@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'
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.
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.