|
Home > Archive > Unix Shell > November 2005 > Regex for white space
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Regex for white space
|
|
| Ying Zu 2005-11-23, 5:54 pm |
|
Suppose a series of white spaces in between a string, just like"
"foo fooo foooo fooooo"
Now I wonder how can I get a new string contains no spaces or substuted by symbols like _
Maybe a simple regex use but I am a newbie in shell script...
thanks 
| |
| William Park 2005-11-23, 5:54 pm |
| Ying Zu <nye@shao.ac.cn> wrote:
>
> Suppose a series of white spaces in between a string, just like"
>
> "foo fooo foooo fooooo"
>
> Now I wonder how can I get a new string contains no spaces or substuted by symbols like _
>
> Maybe a simple regex use but I am a newbie in shell script...
>
> thanks 
man tr (-s)
--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
| |
| Lew Pitcher 2005-11-23, 5:54 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ying Zu wrote:
> Suppose a series of white spaces in between a string, just like"
>
> "foo fooo foooo fooooo"
>
> Now I wonder how can I get a new string contains no spaces or substuted by symbols like _
tr(1) or sed(1) should do
pitchl@srdscs05:~$ echo "foo fooo foooo fooooo" | tr '[:blank:]' '_'
foo_fooo_foooo__fooooo
pitchl@srdscs05:~$ echo "foo fooo foooo fooooo" | tr -d '[:blank:]'
foofooofoooofooooo
pitchl@srdscs05:~$ echo "foo fooo foooo fooooo" \
| sed 's/[[:blank:]]/_/g'
foo_fooo_foooo__fooooo
pitchl@srdscs05:~$ echo "foo fooo foooo fooooo" \
| sed 's/[[:blank:]]//g'
foofooofoooofooooo
> Maybe a simple regex use but I am a newbie in shell script...
>
> thanks 
- --
Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFDhMd+agVFX4UWr64RApb1AKCTiSzvVRgL
B4T414bfNUpJNg7IcQCfZQju
w4MiH/PxbomL5Z46WePz0y8=
=iblw
-----END PGP SIGNATURE-----
| |
| johngnub 2005-11-23, 5:54 pm |
| Depending on how you want it to work, one more option, a bit off topic.
`echo "Foo Bar" |grep "Foo[ ]+Bar" `
the " [ ] " is a char class of spaces, newer OS's support the cool
[:space:] regex options,
Some OS's you may need to use egrep or grep -E for extended regex.
have fun!! JB
| |
| Ed Morton 2005-11-23, 5:54 pm |
|
johngnub wrote:
> Depending on how you want it to work, one more option, a bit off topic.
>
> `echo "Foo Bar" |grep "Foo[ ]+Bar" `
> the " [ ] " is a char class of spaces, newer OS's support the cool
> [:space:] regex options,
> Some OS's you may need to use egrep or grep -E for extended regex.
>
> have fun!! JB
>
please read this before posting again:
http://cfaj.freeshell.org/google
| |
| Dan Mercer 2005-11-23, 8:50 pm |
|
"Ying Zu" <nye@shao.ac.cn> wrote in message news:m0r797rwc5.fsf@shao.ac.cn...
:
: Suppose a series of white spaces in between a string, just like"
:
: "foo fooo foooo fooooo"
:
: Now I wonder how can I get a new string contains no spaces or substuted by symbols like _
:
: Maybe a simple regex use but I am a newbie in shell script...
:
: thanks 
str="Now is the winter of our discontent made glorious summer by this sun of york"
ifs=$IFS
set -- $str
IFS=
x=$*
IFS=_
y=$*
IFS=$ifs
echo "$x\n$y"
Nowisthewinterofourdiscontentmadegloriou
ssummerbythissunofyork
Now_is_the_winter_of_our_discontent_made
_glorious_summer_by_this_sun_of_york
Dan Mercer
| |
| Etienne Marais 2005-11-25, 5:53 pm |
| Dan Mercer wrote:
>
> "Ying Zu" <nye@shao.ac.cn> wrote in message
> news:m0r797rwc5.fsf@shao.ac.cn...
> :
> : Suppose a series of white spaces in between a string, just like"
> :
> : "foo fooo foooo fooooo"
> :
> : Now I wonder how can I get a new string contains no spaces or substuted
> : by symbols like _
> :
> : Maybe a simple regex use but I am a newbie in shell script...
> :
> : thanks 
>
> str="Now is the winter of our discontent made glorious summer by this
> sun of york" ifs=$IFS
> set -- $str
> IFS=
> x=$*
> IFS=_
> y=$*
> IFS=$ifs
> echo "$x\n$y"
> Nowisthewinterofourdiscontentmadegloriou
ssummerbythissunofyork
>
Now_is_the_winter_of_our_discontent_made
_glorious_summer_by_this_sun_of_york[vbc
ol=seagreen]
>
> Dan Mercer[/vbcol]
%> echo "The big fat cat" | sed 's/ //g'
Thebigfatcat
%> echo "The big fat cat" | sed 's/ /_/g'
The_big_fat_cat
Regards
Etienne Marais
| |
| Stephane Chazelas 2005-11-25, 5:53 pm |
| On Fri, 25 Nov 2005 16:35:24 +0200, Etienne Marais wrote:
[...]
> Now_is_the_winter_of_our_discontent_made
_glorious_summer_by_this_sun_of_york
>
> %> echo "The big fat cat" | sed 's/ //g'
> Thebigfatcat
> %> echo "The big fat cat" | sed 's/ /_/g'
> The_big_fat_cat
[...]
Actually, Dan's version doesn't do only that, it also removes
leading and trailing blanks. It convert and *sequence* of blanks
(tab, space, nl) to a "_", not just *every* space.
It also expands wildcards into filenames, and as yours, may do
funny things with backslashes.
For:
str=" foo\n *
bar "
both solutions will give sensibly different results.
--
Stephane
| |
| Dan Mercer 2005-11-25, 5:53 pm |
|
"Etienne Marais" <etienne@cosmiclink.co.za> wrote in message news:dm7704$729$1@ctb-nnrp2.saix.net...
: Dan Mercer wrote:
:
: >
: > "Ying Zu" <nye@shao.ac.cn> wrote in message
: > news:m0r797rwc5.fsf@shao.ac.cn...
: > :
: > : Suppose a series of white spaces in between a string, just like"
: > :
: > : "foo fooo foooo fooooo"
: > :
: > : Now I wonder how can I get a new string contains no spaces or substuted
: > : by symbols like _
: > :
: > : Maybe a simple regex use but I am a newbie in shell script...
: > :
: > : thanks 
: >
: > str="Now is the winter of our discontent made glorious summer by this
: > sun of york" ifs=$IFS
: > set -- $str
: > IFS=
: > x=$*
: > IFS=_
: > y=$*
: > IFS=$ifs
: > echo "$x\n$y"
: > Nowisthewinterofourdiscontentmadegloriou
ssummerbythissunofyork
: >
: Now_is_the_winter_of_our_discontent_made
_glorious_summer_by_this_sun_of_york
: >
: > Dan Mercer
:
: %> echo "The big fat cat" | sed 's/ //g'
: Thebigfatcat
: %> echo "The big fat cat" | sed 's/ /_/g'
: The_big_fat_cat
:
: Regards
: Etienne Marais
But you should know how to do things in the shell. You may not
always have sed, for instance. I bought a Lindows (nka Linspire)
machine from Walmart. It came with almost NO Gnu utilities on it.
I did immediately download ksh93 and was able to eventually
get it working correctly, but a lot of hacking was required to figure
out what to do (without voiding the warranty). Ksh93 was my
Swiss Army knife (TM). I have worked in disaster situations
(corrupted disk) in which the only thing still working WAS the shell
(because it was running when the disk was corrupted by an inept
individual who had been given the root password and told to
"poke around"). I was able to save the day because, if the shell
is good enough, you can get along without sed/awk/grep/etc.
You may also find yourself in a situation where you can't fork another
process due to (well, in my case, the same idiot as above).
To paraphrase, to the man who knows only how to operate a hammer,
the whole world looks like a nail.
Dan Mercer
|
|
|
|
|