find a string from a sentence..

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, [at] #$%

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
krisworld [ Fr, 02 November 2007 18:50 ] [ ID #1860571 ]

Re: find a string from a sentence..

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, [at] #$%
>
> 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.
Ed Morton [ Fr, 02 November 2007 19:05 ] [ ID #1860572 ]

Re: find a string from a sentence..

On Nov 2, 10:05 am, Ed Morton <mor... [at] 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, [at] #$%
>
> > 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...]
krisworld [ Fr, 02 November 2007 19:11 ] [ ID #1860573 ]

Re: find a string from a sentence..

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, [at] #$%
>
> 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, [at] #$%'
expr "x$sentence" : 'x.*\([a-z]-[a-z]-[a-z]-[a-z]\),'


--
Stéphane
Stephane CHAZELAS [ Fr, 02 November 2007 19:28 ] [ ID #1860575 ]

Re: find a string from a sentence..

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, [at] #$%
>
> 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, [at] #$%

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
cfajohnson [ Fr, 02 November 2007 21:12 ] [ ID #1860579 ]

Re: find a string from a sentence..

On 11/2/2007 1:11 PM, krisworld wrote:
> On Nov 2, 10:05 am, Ed Morton <mor... [at] 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, [at] #$%
>>
>>>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.
Ed Morton [ Fr, 02 November 2007 22:07 ] [ ID #1860580 ]

Re: find a string from a sentence..

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... [at] 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, [at] #$%
>>>
>>>>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
Ed Morton [ Fr, 02 November 2007 22:10 ] [ ID #1860581 ]

Re: find a string from a sentence..

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, [at] #$%
>
> 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.
Cyrus Kriticos [ Fr, 02 November 2007 22:18 ] [ ID #1860582 ]

Re: find a string from a sentence..

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, [at] #$%
>
> I am interested to find, a-b-c-d
>
> Using some thing as "[a-z]-[a-z]-[a-z]-[a-z],"

Either

awk -v RS=, '/[a-z]-[a-z]-[a-z]-[a-z]/'

or

awk -v RS=, '/^[a-z]-[a-z]-[a-z]-[a-z]$/'


Janis

>
> Thanks and regards
>
> kris
>
Janis Papanagnou [ Sa, 03 November 2007 04:00 ] [ ID #1861405 ]

Re: find a string from a sentence..

On Nov 2, 11:50 am, krisworld <krishnamurthy.i... [at] gmail.com> 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, [at] #$%
>
> 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

ruby -ne 'puts $& if /([a-z]-){3}[a-z]/'
William James [ Sa, 03 November 2007 08:28 ] [ ID #1861406 ]
Linux » comp.unix.shell » find a string from a sentence..

Vorheriges Thema: wget for downloading scientific papers
Nächstes Thema: [bash] last element of the $* array