I am using the following code to fetch lines that are generated in last 1 hr . Hence, I am using date function to calculate -last 1 hr & the current hr and then somehow use awk (or sed-if someone could guide me better)
with some regex pattern.
awk -v DT1="${dt_1}:[0-9][0-9]:[0-9][0-9] ${dt_2}" -v DT2="${dt_3}:[0-9][0-9]:[0-9][0-9] ${dt_4}" ' {
n = match ( $0, DT1 )
if ( n )
flag = 1
n = match ( $0, DT2 )
if ( n )
flag = 0
} flag ' logfile
The space between Y and l, is the culprit
I had given this space to accomodate 2 digit hour. For 1 digit hr, if there is no space between Y and l, the script works fine.
Otherwise, it does nothing..(as happening with me till now)
Your script worked because there are 2 spaces between the year and the hr in the log file that you have shown:
2013 5
and so it could match exactly.
If you could suggest something here....that would be much appreciated.
How should I modify my date format calculation to match in exact date in log file - bearing in mind 1 digit and 2 digit hr
dt_1=$( date +"%h %d, %Y[ ]*%l:[0-9][0-9]:[0-9][0-9] %p" -d "1 hour ago" )
dt_2=$( date +"%h %d, %Y[ ]*%l:[0-9][0-9]:[0-9][0-9] $p" )
awk -v DT1="${dt_1}" -v DT2="${dt_2}" ' {
n = match ( $0, DT1 )
if ( n )
flag = 1
n = match ( $0, DT2 )
if ( n )
flag = 0
} flag ' logfile
Your guidance helped....The change I made to the scriptlet was that
I omitted '$' sign from dt_1 and d2_2 calculation...and it worked.
Perhaps if you could tell me why you had assigned these variables inside a $ sign.?
Also,I wanted to know...what is the most efficient way to get my script (that I am working on ..) executed on multiple remote servers, one by one and get the result returned (in the form of a file) to my local server ?
something like this on my local server:
for server in `cat $serverlist.txt`
do
ssh user@$server < ./sarah.sh #sarah.sh is a local script
done >> result-on-local-server.txt