find a string from a sentence..

find a string from a sentence..

am 02.11.2007 18:50:49 von krisworld

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

Re: find a string from a sentence..

am 02.11.2007 19:05:46 von Ed Morton

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.

Re: find a string from a sentence..

am 02.11.2007 19:11:01 von krisworld

On Nov 2, 10:05 am, Ed Morton 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...]

Re: find a string from a sentence..

am 02.11.2007 19:28:49 von Stephane CHAZELAS

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

Re: find a string from a sentence..

am 02.11.2007 21:12:20 von cfajohnson

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
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

Re: find a string from a sentence..

am 02.11.2007 22:07:51 von Ed Morton

On 11/2/2007 1:11 PM, krisworld wrote:
> On Nov 2, 10:05 am, Ed Morton 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.

Re: find a string from a sentence..

am 02.11.2007 22:10:03 von Ed Morton

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 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

Re: find a string from a sentence..

am 02.11.2007 22:18:37 von Cyrus Kriticos

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.

Re: find a string from a sentence..

am 03.11.2007 04:00:51 von Janis Papanagnou

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],"

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
>

Re: find a string from a sentence..

am 03.11.2007 08:28:15 von William James

On Nov 2, 11:50 am, 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

ruby -ne 'puts $& if /([a-z]-){3}[a-z]/'