The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 11:00 PM
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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-22-2005
yongho yongho is offline
Registered User
  
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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 04:48 PM..
  #2 (permalink)  
Old 06-22-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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 (permalink)  
Old 06-23-2005
yongho yongho is offline
Registered User
  
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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 09:53 AM..
  #4 (permalink)  
Old 06-23-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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 (permalink)  
Old 06-26-2005
thepicoman thepicoman is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 14
this is eval

you typen in sudo a the biginng of the program coman to override the scynan cheak.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0