LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-08-2011, 02:46 AM   #1
wasim_jd
LQ Newbie
 
Registered: Dec 2011
Posts: 12
Blog Entries: 1

Rep: Reputation: Disabled
[bash/awk] How to increment hour in a loop?


Hi All,
Could you please help me to write theis script for every hour,it should increment on hourly basis.

Code:
awk -F  ","  '{ if (($3 ~ /^965/) && ($14 ~ /2011\/12\/07 00:[0-1][0-9]:[0-9][0-9]/)) print $17}' * |sort | uniq -c |sort -nr |head -10
awk -F  ","  '{ if (($3 ~ /^965/) && ($14 ~ /2011\/12\/07 01:[0-1][0-9]:[0-9][0-9]/)) print $17}' * |sort | uniq -c |sort -nr |head -10
awk -F  ","  '{ if (($3 ~ /^965/) && ($14 ~ /2011\/12\/07 02:[0-1][0-9]:[0-9][0-9]/)) print $17}' * |sort | uniq -c |sort -nr |head -10
...
...
...
soplease help me out...

Thanks in advance..

Last edited by colucix; 12-08-2011 at 11:33 AM. Reason: CODE tags corrected
 
Old 12-08-2011, 02:57 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,301

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
1. can you explain what you are trying to achieve
2. look into cron http://adminschoice.com/crontab-quic...Crontab%20file
 
Old 12-08-2011, 03:22 AM   #3
wasim_jd
LQ Newbie
 
Registered: Dec 2011
Posts: 12
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
bash programming

If you see here "($14 ~ /2011\/12\/07 02:[0-1][0-9]:[0-9][0-9]/)

Hours should get increased 1 for every hr.
I am trying to take data which matches ($3 ~ /^965/) and print by hour basis...


Thanks
Wasim.
 
Old 12-08-2011, 04:00 AM   #4
wasim_jd
LQ Newbie
 
Registered: Dec 2011
Posts: 12
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
check if this works..

Check if this works..
Code:
#!/bin/bash
n==0
while [[ $n -le 23 ]]
do
                  awk -F  ","  '{ if (($3 ~ /^965/) && ($26 ~ /^bulksms/) && ($14 ~ /2011\/12\/07 $n:[0-1][0-9]:[0-9][0-9]/)) print $17}' * |sort | uniq -c |sort -nr |head -10
                  ((n++))
                    done
i executed but i have

./b.sh: line 4: [[: =0: syntax error: operand expected (error token is "=0")

pls do help..

Thanks..
 
Old 12-08-2011, 11:59 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The assignment statement in bash (like in any other programming language) requires a single equal signs. You used two of them, so that the second one has been included in the value of the variable n:
Code:
n==0
when the shell performs the variable substitution in line 4, the test statement results in
Code:
while [[ =0 -le 23 ]]
which is obviously wrong.

Regarding your original question, the problem is how to pass a shell variable to an awk program. Your code will not work as expected because you treat $n as a shell variable inside the awk code, whereas in awk $n will be interpreted as the n-th field. Furthermore take in mind that shell variables are not expanded inside single quotes. For an explanation of the correct way to pass a shell variable, please see http://www.gnu.org/s/gawk/manual/gaw...hell-Variables.

Basically you can use the -v option and assign the value $n to the awk variable n. However in this case the "n" inside the regular expression will be treated literally. In other words, you cannot use awk variables inside a regexp. What you need is a dynamic regexp, e.g.
Code:
awk -v n=$n -F,  '{ if (($3 ~ /^965/) && ($26 ~ /^bulksms/) && ($14 ~ "2011/12/07 " n ":[0-1][0-9]:[0-9][0-9]")) print $17}' file
The alternative is to correctly use a sequence of single and double quotes to write down the command:
Code:
awk -F,  '{ if (($3 ~ /^965/) && ($26 ~ /^bulksms/) && ($14 ~ /2011\/12\/07 '"$n"':[0-1][0-9]:[0-9][0-9]/)) print $17}' file
Here you close the initial single quote when you want to let bash do the variable substitution and re-open single quotes to write down the rest of the awk code. Hope this helps.

Last edited by colucix; 12-08-2011 at 12:11 PM. Reason: Added information.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested loop questions and using a counter to increment array output aSingularity Programming 24 01-19-2012 02:06 PM
bash equivalent to C increment operator andrewb758 Linux - General 5 02-21-2009 12:25 PM
can't put an if statement as an increment in a for loop. Why? japhy Programming 2 02-07-2009 06:07 AM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM
facing problem in increment operator of set :infinite LOOP ashwinipahuja Programming 0 06-03-2004 12:05 AM


All times are GMT -5. The time now is 03:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration