![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| CEP vs. “Business Rules” | iBot | Complex Event Processing RSS News | 0 | 10-23-2007 12:00 AM |
| rules | vrn | Post Here to Contact Site Administrators and Moderators | 2 | 03-21-2006 09:25 AM |
| Rules | Tux | Post Here to Contact Site Administrators and Moderators | 2 | 01-20-2005 02:48 PM |
| rules? | Xskwizitboi | UNIX for Dummies Questions & Answers | 1 | 12-02-2004 01:46 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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.. |
|
||||
|
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. |
|
||||
|
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.. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|