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 > shell script
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

shell script

Réponse
 
LinkBack Outils de la discussion
Vieux 20/07/2007, 13h34   #1
ashu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut shell script

1) shell script that counts number of vowel, consonants and digits in
a string ?
is there any function by which i can read one charcter at a time ,
then i can match it .
or there is another way to do this?

2)a shell script that reads line one by one and if a letter is found
appenned it to file1
and if digit is found appenned to file2. rest are discarded, in end
print total no of line
read and total no of lines lost.lines should be entered in command
line until ctrl-d
pressed.
my queston is how to read enter lines until ctrl d is found. then i
can save those lines
in file and using "while read line and for loop" . how i can
match pattern .


eg.
this is unix
123 132 31 414
//balnk line
another line
ctrl d

how i can match one by one word in line1 and line2, test or grep or
something else.
while read line and for lopp i can use to store a any in my variable
"word"
then how can i check whether it is digit or letter.
thankx for

  Réponse avec citation
Vieux 20/07/2007, 14h49   #2
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script

On 20 Jul., 14:34, ashu <ashishmoury...@gmail.com> wrote:
> 1) shell script that counts number of vowel, consonants and digits in
> a string ?
> is there any function by which i can read one charcter at a time ,
> then i can match it .
> or there is another way to do this?


Sure, one other way is...

awk '{print gsub(/[aeiou]/,"")}'

I am not sure, though, whether your professor will like that solution.

>
> 2)a shell script that reads line one by one and if a letter is found
> appenned it to file1


You can use awk's gsub() function above to remove anything but is not
a number using the inverse pattern /[^0-9]/ and redirect the output to
the respective file...

awk '
{ x=$0; gsub(/[^0-9]/,"",x); print x >"numbers"
x=$0; gsub(/[^a-zA-Z]/,"",x); print x >"letters" }
END { print "total lines processed: " NR }'

Add conditions to ignore empty strings x if necessary.
Use wc -l to count number of lines of any file (or add a
counter in the awk program in case x is non-empty).

> and if digit is found appenned to file2. rest are discarded, in end
> print total no of line
> read and total no of lines lost.lines should be entered in command
> line until ctrl-d
> pressed.
> my queston is how to read enter lines until ctrl d is found. then i
> can save those lines
> in file and using "while read line and for loop" . how i can
> match pattern .


while -r read line
do case $line in
pattern1) case_1 ;;
pattern2) case_2 ;;
*) default_case ;;
esac
done

> eg.
> this is unix
> 123 132 31 414
> //balnk line
> another line
> ctrl d
>
> how i can match one by one word in line1 and line2, test or grep or
> something else.


One by one word, e.g. by

while -r read line
do
set -- $line
for word in "$@"
do
: now work on $word
done
done

> while read line and for lopp i can use to store a any in my variable
> "word"
> then how can i check whether it is digit or letter.


See case statement above. (See also the shell docs about the POSIX
character classes, any about supported regular expressions.)

> thankx for


And I suggest to read any book about the basic shell features.

Janis

  Réponse avec citation
Vieux 20/07/2007, 15h31   #3
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script

2007-07-20, 05:34(-07), ashu:
> 1) shell script that counts number of vowel, consonants and digits in
> a string ?
> is there any function by which i can read one charcter at a time ,
> then i can match it .
> or there is another way to do this?


To my knowledge, there's no standard way to know whether a given
character of a given (the user's as defined by LC_CTYPE for
instance) charset is a vowel or consonant. Even in a given
charset, people can disagree on what a consonant is, even in as
simple a charset as ASCII (some people don't consider "y" as a
vowel for instance).

> 2)a shell script that reads line one by one and if a letter is found
> appenned it to file1
> and if digit is found appenned to file2. rest are discarded, in end
> print total no of line
> read and total no of lines lost.lines should be entered in command
> line until ctrl-d
> pressed.
> my queston is how to read enter lines until ctrl d is found. then i
> can save those lines
> in file and using "while read line and for loop" . how i can
> match pattern .

[...]

There is however a standard way to know whether a character is a
letter or digit. It is via the regular expression character
classes [:alpha:] and [:digit:].

It looks like awk is the tool you want to use in that case.

--
Stéphane
  Réponse avec citation
Vieux 20/07/2007, 15h40   #4
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script

Janis wrote:

Typo; should have been...

>
> One by one word, e.g. by
>
> while -r read line


while read -r line

> do
> set -- $line
> for word in "$@"
> do
> : now work on $word
> done
> done

  Réponse avec citation
Vieux 20/07/2007, 16h16   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script

2007-07-20, 16:40(+02), Janis Papanagnou:
> Janis wrote:
>
> Typo; should have been...
>
>>
>> One by one word, e.g. by
>>
>> while -r read line

>
> while read -r line


while IFS= read -r line

>> do
>> set -- $line
>> for word in "$@"


If you're going to leave a variable unquoted, then you should
define IFS and disable filename generation.

The above two lines can be made

for word in $line

>> do
>> : now work on $word
>> done
>> done


In any case you don't want to do test processing that way.

--
Stéphane
  Réponse avec citation
Vieux 20/07/2007, 19h51   #6
ashu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script

On Jul 20, 8:16 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote:
> 2007-07-20, 16:40(+02), Janis Papanagnou:
>
> > Janis wrote:

>
> > Typo; should have been...

>
> >> One by one word, e.g. by

>
> >> while -r read line

>
> > while read -r line

>
> while IFS= read -r line
>
> >> do
> >> set -- $line
> >> for word in "$@"

>
> If you're going to leave a variable unquoted, then you should
> define IFS and disable filename generation.
>
> The above two lines can be made
>
> for word in $line
>
> >> do
> >> : now work on $word
> >> done
> >> done

>
> In any case you don't want to do test processing that way.
>
> --
> Stéphane


i m neewbie and very confused, can anyone give me simple shell script
to solve those questions

  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 08h22.


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19673 seconds with 14 queries