PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > find a string from a sentence..
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

find a string from a sentence..

Réponse
 
LinkBack Outils de la discussion
Vieux 02/11/2007, 18h50   #1
krisworld
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,@#$%

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

  Réponse avec citation
Vieux 02/11/2007, 19h05   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,@#$%
>
> 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.

  Réponse avec citation
Vieux 02/11/2007, 19h11   #3
krisworld
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: find a string from a sentence..

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

  Réponse avec citation
Vieux 02/11/2007, 19h28   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,@#$%
>
> 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
  Réponse avec citation
Vieux 02/11/2007, 21h12   #5
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,@#$%
>
> 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
  Réponse avec citation
Vieux 02/11/2007, 22h07   #6
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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...@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.

  Réponse avec citation
Vieux 02/11/2007, 22h10   #7
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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...@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

  Réponse avec citation
Vieux 02/11/2007, 22h18   #8
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,@#$%
>
> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h46.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14352 seconds with 16 queries