Syntax/Rules (Can I do this?)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Syntax/Rules (Can I do this?)
# 1  
Old 06-22-2005
Syntax/Rules (Can I do this?)

I have a small piece of code that I need to iterate 24 times (for each hour of a day).... here's what it looks like now...

Code:
while read hour file; do
       
    if [ $hour -eq 00 ]; then
        count=`awk -F, 'BEGIN { count=0 } ............. { count++ } END { print count }' $file`
        hourly01=`expr $hourly1 + $trade_count`
    elif [ $hour -eq 01 ]; then
        count=`awk -F, 'BEGIN { count=0 } ............. { count++ } END { print count }' $file`
        hourly02=`expr $hourly2 + $trade_count`
    ..................
    fi

done < ls.txt

As you can see it's simple, and iterates over and over, but unneccessarily (24 times)... Could I replace all the numbers with one variable... like such..

don't take my code letter for letter, just take the idea and tell me if it can be done..

Code:
for (x=00; x<=23; x++) {
    if [ $hour -eq $x ]; then
        count=`awk -F, 'BEGIN { count=0 } ............. { count++ } END { print count }' $file`
        hourly$x=`expr ${hourly}$x + $trade_count`
    fi
}

Thats' the idea.. I know that I didn't use a unix for-loop, but I just started learning unix and I didn't check what the for looks like in unix yet).. But can I write the middle section (every occurence of $x) the way that I wrote it here?

Last edited by yongho; 06-22-2005 at 05:48 PM..
# 2  
Old 06-22-2005
awk supports arrays.

The
Code:
'{ count=0 } ............. { count++ } END { print count }'

part could write to an array element instead of "print count". Instead of going crazy over this, consider a good book like the O'Reilly book 'sed & awk' If you're in Manhattan, Barnes and Noble has it.

Short answer: in awk you can do what you asked. All in one piece of awk code.
# 3  
Old 06-23-2005
I see

I'm now considering doing it in the way you suggested, but I have another question about your suggested method.

My problem: Originally I ran something like ls -al | grep Jun 22 | cut -c49-100 >> myLs.txt to give me just the time and filename of all files for June 22nd in a text file.

With that text file I began awking.

You're suggesting that instead of using while read hour file that I begin the awk right away, to avoid repeating all that code that I did.

Since the text file with the LS holds the filenames of CSV files, I need to be able to, at some point, awk those files to perform some count calculations. I don't think I could use a nested awk.. awk within an awk to open those files found in the first awk. (or could I?).

I'll pick up the book over the weekend, thanks.

Last edited by yongho; 06-23-2005 at 10:53 AM..
# 4  
Old 06-23-2005
awk supports the system() function just like in C and php.

You can create a dynamic string, then pass it to system to create output.

It's not that awk is better than script or vice-versa - it's just bad to keep using several different tools and bouncing back and forth among them. IMO.
# 5  
Old 06-26-2005
this is eval

Smilie you typen in sudo a the biginng of the program coman to override the scynan cheak.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. What is on Your Mind?

Forum rules

After reading the FAQ for the first time, I noticed Rule # 14 : :D:D:D Anyways, I hope to make more use of this forum in future, and also to help others. Regards Ook. :) (0 Replies)
Discussion started by: The_Librarian
0 Replies

2. Post Here to Contact Site Administrators and Moderators

rules

rules are there but asking 2 questions out of 30 is surely understanable esp when the instructor gives an open book test and urged us to seek answers anywhere we can except from him directly. (2 Replies)
Discussion started by: vrn
2 Replies

3. Post Here to Contact Site Administrators and Moderators

Rules

https://www.unix.com/showthread.php?t=2971 Spelling Error. You 'Adhere' to rules, not adhear. (2 Replies)
Discussion started by: Tux
2 Replies

4. UNIX for Dummies Questions & Answers

rules?

What rules? I have been searching for hours on the internet and just cannot seem to find the command you would type to add a serial port or the file that specifies whether a filesystem shoudl be mounted at boot time or not............. (1 Reply)
Discussion started by: Xskwizitboi
1 Replies
Login or Register to Ask a Question