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...
|
In which language?
|
Sed, tr
|
using bash shell, no need external commands
Code:
$ string="my string with spaces and - dashes - dashes" |
The request was to remove whitespace - better to use a class rather than a single space character.
|
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 |
Quote:
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' |
@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) |
Quote:
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 |
Quote:
Quote:
Quote:
Code:
string="my string with spaces and - dashes - dashes" |
Quote:
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" |
i think i have confused you, sorry, this part
Code:
string="my<tabs tabs> string with spaces and - dashes - dashes" |
Quote:
Quote:
Code:
c:~$ string=$'my\t 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//[ -]/} Of course, this is just an alternative. If you may like, there's also character class you can use Code:
echo ${string//[[:space:]]/} |
Quote:
|
All times are GMT -5. The time now is 08:30 AM. |