|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
hi all
I just wonder, is their any way to find a string from a sentence, using a patten. say I have the sentence as abcd,123,a-b-c-d,0-1-2-3-4,@#$% I am interested to find, a-b-c-d Using some thing as "[a-z]-[a-z]-[a-z]-[a-z]," Thanks and regards kris |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 11/2/2007 12:50 PM, krisworld wrote: > hi all > I just wonder, is their any way to find a string from a sentence, > using a patten. > > say > I have the sentence as > > abcd,123,a-b-c-d,0-1-2-3-4,@#$% > > I am interested to find, a-b-c-d > > Using some thing as "[a-z]-[a-z]-[a-z]-[a-z]," > > Thanks and regards > > kris > Use grep -o with that pattern, if your grep supports "-o". Ed. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Nov 2, 10:05 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/2/2007 12:50 PM, krisworld wrote: > > > > > > > hi all > > I just wonder, is their any way to find a string from a sentence, > > using a patten. > > > say > > I have the sentence as > > > abcd,123,a-b-c-d,0-1-2-3-4,@#$% > > > I am interested to find, a-b-c-d > > > Using some thing as "[a-z]-[a-z]-[a-z]-[a-z]," > > > Thanks and regards > > > kris > > Use grep -o with that pattern, if your grep supports "-o". > > Ed.- Hide quoted text - > > - Show quoted text - grep: illegal option -- o Usage: grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...] grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]... [-f pattern_file]... [file...] grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...] grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...] grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...] grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...] |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
2007-11-02, 10:50(-07), krisworld:
[...] > I just wonder, is their any way to find a string from a sentence, > using a patten. > > say > I have the sentence as > > abcd,123,a-b-c-d,0-1-2-3-4,@#$% > > I am interested to find, a-b-c-d > > Using some thing as "[a-z]-[a-z]-[a-z]-[a-z]," [...] sentence='abcd,123,a-b-c-d,0-1-2-3-4,@#$%' expr "x$sentence" : 'x.*\([a-z]-[a-z]-[a-z]-[a-z]\),' -- Stéphane |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 2007-11-02, krisworld wrote:
> > > hi all > I just wonder, is their any way to find a string from a sentence, > using a patten. > > say > I have the sentence as > > abcd,123,a-b-c-d,0-1-2-3-4,@#$% > > I am interested to find, a-b-c-d > > Using some thing as "[a-z]-[a-z]-[a-z]-[a-z]," sentence=abcd,123,a-b-c-d,0-1-2-3-4,@#$% case $sentence in *[a-z]-[a-z]-[a-z]-[a-z],*) left=${sentence%%[a-z]-[a-z]-[a-z]-[a-z],*} right=${sentence#*[a-z]-[a-z]-[a-z]-[a-z],} temp=${sentence%"$right"} printf "%s\n" "${temp#"$left"}" ;; *) echo not found >&2 ;; esac (It's longer but much faster than using expr.) -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 11/2/2007 1:11 PM, krisworld wrote: > On Nov 2, 10:05 am, Ed Morton <mor...@lsupcaemnt.com> wrote: > >>On 11/2/2007 12:50 PM, krisworld wrote: >> >> >> >> >> >> >>>hi all >>>I just wonder, is their any way to find a string from a sentence, >>>using a patten. >> >>>say >>>I have the sentence as >> >>>abcd,123,a-b-c-d,0-1-2-3-4,@#$% >> >>>I am interested to find, a-b-c-d >> >>>Using some thing as "[a-z]-[a-z]-[a-z]-[a-z]," >> >>>Thanks and regards >> >>>kris >> >>Use grep -o with that pattern, if your grep supports "-o". >> >> Ed.- Hide quoted text - >> >>- Show quoted text - > > > grep: illegal option -- o > Usage: grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...] > grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]... [-f > pattern_file]... [file...] > grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...] > grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f > pattern_file]... [file...] > grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...] > grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f > pattern_file]... [file...] > get GNU grep. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On 11/2/2007 4:07 PM, Ed Morton wrote: > > On 11/2/2007 1:11 PM, krisworld wrote: > >>On Nov 2, 10:05 am, Ed Morton <mor...@lsupcaemnt.com> wrote: >> >> >>>On 11/2/2007 12:50 PM, krisworld wrote: >>> >>> >>> >>> >>> >>> >>> >>>>hi all >>>>I just wonder, is their any way to find a string from a sentence, >>>>using a patten. >>> >>>>say >>>>I have the sentence as >>> >>>>abcd,123,a-b-c-d,0-1-2-3-4,@#$% >>> >>>>I am interested to find, a-b-c-d >>> >>>>Using some thing as "[a-z]-[a-z]-[a-z]-[a-z]," >>> >>>>Thanks and regards >>> >>>>kris >>> >>>Use grep -o with that pattern, if your grep supports "-o". >>> >>> Ed.- Hide quoted text - >>> >>>- Show quoted text - >> >> >>grep: illegal option -- o >>Usage: grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...] >> grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]... [-f >>pattern_file]... [file...] >> grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...] >> grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f >>pattern_file]... [file...] >> grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...] >> grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f >>pattern_file]... [file...] >> > > > get GNU grep. > or use sed: sed -n '/.*\([a-z]-[a-z]-[a-z]-[a-z]\).*/\1/p' file |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
krisworld wrote:
> > I just wonder, is their any way to find a string from a sentence, > using a patten. > > say > I have the sentence as > > abcd,123,a-b-c-d,0-1-2-3-4,@#$% > > I am interested to find, a-b-c-d > > Using some thing as "[a-z]-[a-z]-[a-z]-[a-z]," $ echo "$sentence" | sed 's/.*\([a-z]-[a-z]-[a-z]-[a-z]\).*/\1/' a-b-c-d -- Best regards | Be nice to America or they'll bring democracy to Cyrus | your country. |
|
![]() |
| Outils de la discussion | |
|
|