Programming logic help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Programming logic help
# 1  
Old 05-28-2013
Programming logic help

Hi,

I was having a problem regarding this thread:
HTML Code:
https://www.unix.com/shell-programming-scripting/225221-setting-variable-using-variables-loop.html
I have not been able to solve that issue. So i was thinking maybe my logic to solve the overall problem is wrong, so i am looking for a bit of help on what would be a good way to approach the following task.

I have a list of 24 files that are produced daily. On some days all 24 files may not be produced. The filenames are in the format filename-2digitnumber ( e.g. log-01). In each of the files, i need to search for an occurrence of a particular word and see how many times it occurs. Once i have the occurrences per file, i then need to total up all the occurrences per file into a grand total.

The way i have gone about this was to do a for loop, which had an if statement within it which did a check to see if the file existed. Then I would do a count on the file and assign it to a variable, that is also a variable. I get stuck at this stage as in the above thread. Code example:

Code:
file=log

for i in 01 02 03; do
if [ -f $file-$i ]; then
count$i=`grep -c word $file-$i`
fi
done

for i in 01 02 03; do 
if [ ! -z "$count$i" ]; then
:
else Tcount=`expr $Tcount + $count$i`
fi
done

Any ideas on how to go about this will be appreciated.

Thanks
# 2  
Old 05-28-2013
How about this
Code:
for file in `ls -1 log-[0-9][0-9]`
do
    count=`grep -cw word $file|wc -l 2>/dev/null`
    [ -z "$count" ] && count=0
    sumcount=$(( $sumcount + $count ))
done

echo "Sum of count :$sumcount"

# 3  
Old 05-28-2013
Nothing in your post indicates that you are using the per file counts for anything but calculation. If only the grand total is of interest:
Code:
cat log-* | grep -c word

If there are unwanted files in the directory that match log-*, you'll need to refine the pattern (possibly using more than one).

Regards,
Alister

---------- Post updated at 08:52 AM ---------- Previous update was at 08:47 AM ----------

Quote:
Originally Posted by pravin27
Code:
for file in `ls -1 log-[0-9][0-9]`

That is never necessary and it will choke on filenames with IFS characters. The following is both safe and efficient:
Code:
for file in log-[0-9][0-9]

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

More a logic than a programming question

hello, I'm a littlebit lost... i have a table of single names and combination of two names and i want a table of ALL single names with ALL combinations (also the indirect ones). - - - in reality the names are longer, not only a,b,.. and the table is tab-delimited, and the result table... (6 Replies)
Discussion started by: dietmar13
6 Replies

2. Shell Programming and Scripting

need a logic for awk programming

Hello Friends, I have a txt file like below //*Init Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Init End //*Main Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Main End I want to calculate the values between //* Init Start & //* Init End And //*Main Start & //*Main... (5 Replies)
Discussion started by: user_prady
5 Replies
Login or Register to Ask a Question