|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|