The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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-22-2007 08:00 PM
rules vrn Post Here to Contact Site Administrators and Moderators 2 03-21-2006 05:25 AM
Rules Tux Post Here to Contact Site Administrators and Moderators 2 01-20-2005 10:48 AM
rules? Xskwizitboi UNIX for Dummies Questions & Answers 1 12-02-2004 09:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-22-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 01:48 PM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-22-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,279
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 06-23-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 06:53 AM.
Reply With Quote
  #4 (permalink)  
Old 06-23-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,279
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #5 (permalink)  
Old 06-26-2005
Registered User
 

Join Date: Jun 2005
Posts: 14
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
this is eval

you typen in sudo a the biginng of the program coman to override the scynan cheak.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101