LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Specific range of records (https://www.linuxquestions.org/questions/linux-newbie-8/specific-range-of-records-791195/)

dina3e 02-24-2010 02:48 AM

Specific range of records
 
I am having a log file where there is some discontinuous of dates are there but if i go for range of date to find the records, if is some date is missing the result is displaying full file list( Start date to end of file ) How could i restrict the result upto the mentioned date range.

Code:

$cat sample.log
Jan 03 -error    yyyy-1234
Jan 04 -error    uuuu-4563
Jan 04 -debug    kkkk-8765
Jan 05 -error    jnjl-3210
Jan 08 -error    nmnm-6543
Jan 15 -error    hhhh-6321
Feb 02 -debug    ftrd-5439

$cat range.sh
echo -e" Please provide the log file\n"
read file
echo -e " Plese give start date\n"
read st_dt
echo -e " Please give End date \n"
read en_dt
sed -n "/$st_dt/,/$en_dt/p" $file | sed -n "/error/p"

When ever i am giving the existing date it works for me. If i am giving some date like start date = Jan 04 and end date = Jan 14 the script go for full search of pattern and printing the whole file . But my requirement is It should print with in Jan 15 ,Similarly if i go for start date =Jan 07 mean it search from Jan 08 on word to end date.

Tinkster 02-24-2010 10:30 AM

Your problem is quite obvious, and won't be trivially solved
with sed. I'd recommend using awk for this job, which will give
you the possibility to check for the "range" numerically with
a -lt or -ge like option.

dina3e 02-24-2010 11:10 PM

Need help...in awk ??
 
I am very glad to see your reply, How could we go for a comparison of one date with other is there any function available in AWK which could do the job.

Tinkster 02-25-2010 11:46 AM

Having thought about this some more ... what are your chances of
changing the date format in the log file? It looks like it's out
of a custom app .... your life would be so much easier if you just
had the months in a 01..12 notation as well.

Imagine 0108 (Jan 8th) to 0203 {Feb 3rd)

Code:

awk '{if($1 >= 108 && $1 < 204({print}}' sample.log
If you had the same range with names as long as they're subsequent
you could resort to something like this:
Code:

awk '/Jan/,/Feb/{if($1 == "Jan" && $2 >= 08){print};if($1 == "Feb" && $2 < 3){print}}' sample.log
Of course that wouldn't work the very instant you want to search
from Jan to Mar, and the comparison sstring you'd have to build would
become increasingly complicated ...
Code:

awk '/Jan/,/Mar/{if(($1 == "Jan" && $2 >= 08)||$1="Feb"){print};if($1 == "Mar" && $2 < 3){print}}' sample.log
and so forth ... nasty.

dina3e 03-01-2010 02:21 AM

Data range using AWK
 
Hello guru...
i have a log file of this format. i need the out put from specific date range having errors . That date range is the input variables so how could i achieve .Sample log and the codes.

$cat sample_txt.log
Jan 17 15:39:03 [error] LocalDirectory listFiles(): listing files from dir
Jan 17 15:39:03 [error] fm_log:_f_Push() _w_fm_log_export_ftph(): Files f
Jan 17 05:58:35 [debug] fm_log_autoexport 1
Jan 18 05:58:35 [debug] fm_log_autoexport 2/var/www/html/sites/default/mods/_
Jan 18 05:58:36 [error] Channel getIntByNode(): getting _chan from
Jan 18 05:58:36 [debug] fm_log:_f_Push() Array
Jan 18 05:58:36 [notice]fm_log:_f_Push() _w_fm_log_export_active_ftppush__c
Jan 18 05:58:36 [error] LocalDirectory isDirectory(): directory '/opt/_d_/
Feb 23 14:32:01 [debug] fm_log_mod Inside log_nodeapi me..
Feb 23 14:32:02 [error] fm_log_mod Inside log_nodeapi me..
Feb 23 14:32:02 [debug] fm_log_mod Inside log_nodeapi me..
Feb 25 13:54:32 [debug] fm_log_mod Inside log_nodeapi me..
Feb 25 13:54:32 [error] fm_log_mod Inside log_nodeapi me..
Feb 25 13:22:44 [notice]fm_log_mod Inside log_nodeapi me..
Feb 25 14:22:34 [error] fm_log_mod Inside log_nodeapi me..
Feb 27 13:22:44 [debug] fm_log_mod Inside log_nodeapi me..
.
..
Feb 28 13:22:44 [debug] fm_log_mod Inside log_nodeapi me..


$cat log_check.sh
#!/bin/sh
echo -e " Please provide the log file"
read in_file

echo -e " Plese give start date\n"
read st_dt

echo -e " Please give End date \n"
read en_dt

awk "/$st_dt/" ' { f=1; }
"/$en_dt/" {c=1; }
c&&! "/$en_dt/" { c=0;f=0; }
(f||c) && /error/ { Arr[$1" "$2]++ }
END { for (i in Arr) print i" "Arr[i]" error"; }'"$in_file"


In log_check.sh if u replace $dt_dt & $st_dt by some date mean Jan 18 & Feb 25 then it is giving correct answer.

awk '/Jan 18/ { f=1; }
/Feb 25/ {c=1; }
c&&!/Feb 05/ { c=0;f=0; }
(f||c) && /rror/ { Arr[$1" "$2]++ }
END { for (i in Arr) print i" "Arr[i]" error"; }' file
So it is not possible to change the code each time how could i do this with variable substitution in awk , i had gone awk -v for variable substitution but finding difficulties, Need your suggestion .

TB0ne 03-01-2010 10:03 AM

Quote:

Originally Posted by dina3e (Post 3880711)
Hello guru...
i have a log file of this format. i need the out put from specific date range having errors . That date range is the input variables so how could i achieve .Sample log and the codes.

$cat sample_txt.log
Jan 17 15:39:03 [error] LocalDirectory listFiles(): listing files from dir
Jan 17 15:39:03 [error] fm_log:_f_Push() _w_fm_log_export_ftph(): Files f
Jan 17 05:58:35 [debug] fm_log_autoexport 1
Jan 18 05:58:35 [debug] fm_log_autoexport 2/var/www/html/sites/default/mods/_
Jan 18 05:58:36 [error] Channel getIntByNode(): getting _chan from
Jan 18 05:58:36 [debug] fm_log:_f_Push() Array
Jan 18 05:58:36 [notice]fm_log:_f_Push() _w_fm_log_export_active_ftppush__c
Jan 18 05:58:36 [error] LocalDirectory isDirectory(): directory '/opt/_d_/
Feb 23 14:32:01 [debug] fm_log_mod Inside log_nodeapi me..
Feb 23 14:32:02 [error] fm_log_mod Inside log_nodeapi me..
Feb 23 14:32:02 [debug] fm_log_mod Inside log_nodeapi me..
Feb 25 13:54:32 [debug] fm_log_mod Inside log_nodeapi me..
Feb 25 13:54:32 [error] fm_log_mod Inside log_nodeapi me..
Feb 25 13:22:44 [notice]fm_log_mod Inside log_nodeapi me..
Feb 25 14:22:34 [error] fm_log_mod Inside log_nodeapi me..
Feb 27 13:22:44 [debug] fm_log_mod Inside log_nodeapi me..
.
..
Feb 28 13:22:44 [debug] fm_log_mod Inside log_nodeapi me..


$cat log_check.sh
#!/bin/sh
echo -e " Please provide the log file"
read in_file

echo -e " Plese give start date\n"
read st_dt

echo -e " Please give End date \n"
read en_dt

awk "/$st_dt/" ' { f=1; }
"/$en_dt/" {c=1; }
c&&! "/$en_dt/" { c=0;f=0; }
(f||c) && /error/ { Arr[$1" "$2]++ }
END { for (i in Arr) print i" "Arr[i]" error"; }'"$in_file"


In log_check.sh if u replace $dt_dt & $st_dt by some date mean Jan 18 & Feb 25 then it is giving correct answer.

awk '/Jan 18/ { f=1; }
/Feb 25/ {c=1; }
c&&!/Feb 05/ { c=0;f=0; }
(f||c) && /rror/ { Arr[$1" "$2]++ }
END { for (i in Arr) print i" "Arr[i]" error"; }' file
So it is not possible to change the code each time how could i do this with variable substitution in awk , i had gone awk -v for variable substitution but finding difficulties, Need your suggestion .

This is very similar to other threads that you've opened, for the same problem.

Have you tried splitting the $st_dt and en_dt into different variables, then combining them? You may also have a problem with the input variable having a space in it.

MTK358 03-01-2010 10:43 AM

I don't understand exactly what you want to do. Do you want to extract the lines with '[error]' or what?


All times are GMT -5. The time now is 07:11 PM.