LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 02-24-2010, 03:48 AM   #1
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Rep: Reputation: 16
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.
 
Old 02-24-2010, 11:30 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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.
 
Old 02-25-2010, 12:10 AM   #3
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
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.

Last edited by dina3e; 02-25-2010 at 01:51 AM.
 
Old 02-25-2010, 12:46 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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.

Last edited by Tinkster; 02-25-2010 at 12:50 PM.
 
Old 03-01-2010, 03:21 AM   #5
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
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 .
 
Old 03-01-2010, 11:03 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,272

Rep: Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440
Quote:
Originally Posted by dina3e View Post
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.
 
Old 03-01-2010, 11:43 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

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


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
iptables and IP range ban on a specific adapter alpha_hack Linux - Networking 5 09-16-2009 07:54 PM
[SOLVED] SED and Replacing Specific occurrence or Range of Lines bridrod Linux - Newbie 7 08-27-2009 10:59 AM
ANSI C: force malloc to return address with specific range. Neuzen Programming 2 01-04-2009 04:14 PM
Scanning memory range for specific bytes w/ GDB jrtayloriv Programming 5 03-17-2008 03:27 PM
deleting by date a specific range of files nooks12 Linux - Newbie 2 01-08-2008 03:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration