|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
Case Statement
I'm real new to unix scripts. Here's my problem.
I have a text file that holds data in the format of: DON RENTA DON RENTA EON RENTA XON RENTANT ETC.... I am trying to run through each line of the data, cut out the first letter, compare it in a case and count the number of times each letter appears. So far I have: a=0 while read line do a=`cut -c1` case $a in D)echo $a;; *) ;; esac done < temp.txt This produces no output. To test to make sure that my variables work and that the while loop works i changed it to: a=0 while read line do a=`cut -c1` case $a in D);; *)echo $a ;; esac done < temp.txt and it printed out the first letter of each row even if the letter was D. So i can only assume that the issue is my case does not compare a$ and D as being true. If i can get this fixed I am sure that I can do the counting very easily. Is there a glaring mistake that I am missing? Thanks in advance. |
|
#2
|
|||
|
|||
|
your way: a=`echo $line | cut -c1`
my way: forgot all that, use an oneliner, try sed -n 's/^\([^D]\).*/\1/p' inputfile |
|
#3
|
|||
|
|||
|
Thanks. Both work and I even understand the first one. My problem now is counting. I took :
case $a in D) echo $a;; and changed it to case a$ in D) count=`expr $count +1`;; but when i echo $count it is zero. |
|
#4
|
|||
|
|||
|
Another solution without the need for external programs:
Code:
case "$a" in D*) count=`expr $count +1`;; 1. Note: the asterisk. It works like file system wildcards (not regexp) 2. Is your shell "bash"? You can write most scripts in bash without the need for any external programs, eg. count=$((count+1)) 3. If portability is an issue, you should use "sed", "cut" and "expr". They are available by default on most (all?) unices. Bash is not. ... Your $count is probably zero because "expr" wants a space betwen the "+" and the numbers on each side. hth, M.
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#5
|
|||
|
|||
|
Thanks. But there still seems to be a problem somewhere. The full code is:
Code:
count=0 while read line do a=`echo $line | cut -c1` case $a in D)count=`expr $count + 1`;; *);; esac done < temp.txt echo $count But all i get at the end is an ouput of 0 when i know the output should be in the 100's. |
|
#6
|
|||
|
|||
|
Quote:
Odd. The script works fine for me... Maybe a permissions issue or search path now? |
|
#7
|
|||
|
|||
|
Whether or not that code will work depends on the shell used to run it. With a modern shell like ksh it should work.
But the old bourne shell will place the while statement in a subshell. The changes the the count variable will be lost when the subshell exits. |
|
#8
|
|||
|
|||
|
Q to Otts21, is that
A) an exercise to play with shell OR B) a production issue ? case A i propose: for line in `cat inputfile` do case $line in D*) count=`expr $count + 1` ;; continue ;; esac printf "%.1s\n",$line # not tested, should work, my *nix is down done case B: # prints first char of each line NOT starting by D sed -n 's/^\([^D]\).*/\1/p' inputfile # counts lines starting by a D grep -c '^D' inputfile sure, this way open twice 'inputfile' for reading, but 'sed' and 'grep' are very performat 'c' progs, this combination will be faster then every shell. finally, if you want do the job in a ONEloop, use perl or 'c' awk is also a possibility, but on large inputfile it will be slow. |
|
#9
|
|||
|
|||
|
Quote:
This would be my problem then. I made a change to: Code:
while read line do a=`echo $line | cut -c1` case $a in D)count=`expr $count + 1`; echo $count;; *);; esac done < temp.txt echo $count When i run the script now it counts all the way up and then when the while loop is finnished resets the variable and prints 0. example: 1 2 3 4 5 6 7 0 Is there a way to get the number in the variable out of the loop? |
|
#10
|
|||
|
|||
|
Quote:
"Stop using an antique shell" comes quickly to mind. Or write the value of count into a temporary file each iteration of the loop. Outside the loop, read the file to get the value. This is not as terrible as it sounds due to the unix buffer cache. But it's not an ideal solution either. What OS are you using? If it's Solaris, ksh is available. |
|
#11
|
|||
|
|||
|
Well i sent it to a file and overwrote that file until the loop was done. Then cat file to screen. It works. Unfortunatly I am not able to use any other shell as this is at work and is all i have access to.
Thank you everyone for your help. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Case Statement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|