Sed range problem if the last pattern is not met
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I am doing a range search with sed. I want to parse the log data from the date and time 2016-09-29 01:00
to 2016-09-29 01:30
. That is why I have been using the following command,
$ sed -n '/2016-09-29 01:/,/2016-09-29 01:30:.*$/p'
But problem is if 1:30 is not available in log then it returns all the logs to the end.
So how can work with this so that if 1:30
doesn't exist it will go to the just next record not till end.
Things to consider: Logs contains stack trace so lines contain stack trace doesn't start with the date.
sed regular-expression date
add a comment |Â
up vote
0
down vote
favorite
I am doing a range search with sed. I want to parse the log data from the date and time 2016-09-29 01:00
to 2016-09-29 01:30
. That is why I have been using the following command,
$ sed -n '/2016-09-29 01:/,/2016-09-29 01:30:.*$/p'
But problem is if 1:30 is not available in log then it returns all the logs to the end.
So how can work with this so that if 1:30
doesn't exist it will go to the just next record not till end.
Things to consider: Logs contains stack trace so lines contain stack trace doesn't start with the date.
sed regular-expression date
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am doing a range search with sed. I want to parse the log data from the date and time 2016-09-29 01:00
to 2016-09-29 01:30
. That is why I have been using the following command,
$ sed -n '/2016-09-29 01:/,/2016-09-29 01:30:.*$/p'
But problem is if 1:30 is not available in log then it returns all the logs to the end.
So how can work with this so that if 1:30
doesn't exist it will go to the just next record not till end.
Things to consider: Logs contains stack trace so lines contain stack trace doesn't start with the date.
sed regular-expression date
I am doing a range search with sed. I want to parse the log data from the date and time 2016-09-29 01:00
to 2016-09-29 01:30
. That is why I have been using the following command,
$ sed -n '/2016-09-29 01:/,/2016-09-29 01:30:.*$/p'
But problem is if 1:30 is not available in log then it returns all the logs to the end.
So how can work with this so that if 1:30
doesn't exist it will go to the just next record not till end.
Things to consider: Logs contains stack trace so lines contain stack trace doesn't start with the date.
sed regular-expression date
sed regular-expression date
edited Aug 29 at 11:23
asked Aug 29 at 11:03
muhammad
499514
499514
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Not so strange. sed
is a stream editor, it processes lines as they come. A range like /a/,/b/
means the lines are selected as soon as a
is found, and no-longer selected after b
is found. If b
is never found, it never stops selecting lines.
Here, you should rather use awk
instead. Assuming those timestamps are at the beginning of the line:
awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'
Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.
Another approach to work around that would be:
awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
$0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'
That is use a range like in sed
, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.
Worked like charm. But then I saw that it doesn't include stack traces.
â muhammad
Aug 29 at 11:22
@muhammad, see edit.
â Stéphane Chazelas
Aug 29 at 11:34
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Not so strange. sed
is a stream editor, it processes lines as they come. A range like /a/,/b/
means the lines are selected as soon as a
is found, and no-longer selected after b
is found. If b
is never found, it never stops selecting lines.
Here, you should rather use awk
instead. Assuming those timestamps are at the beginning of the line:
awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'
Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.
Another approach to work around that would be:
awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
$0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'
That is use a range like in sed
, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.
Worked like charm. But then I saw that it doesn't include stack traces.
â muhammad
Aug 29 at 11:22
@muhammad, see edit.
â Stéphane Chazelas
Aug 29 at 11:34
add a comment |Â
up vote
2
down vote
accepted
Not so strange. sed
is a stream editor, it processes lines as they come. A range like /a/,/b/
means the lines are selected as soon as a
is found, and no-longer selected after b
is found. If b
is never found, it never stops selecting lines.
Here, you should rather use awk
instead. Assuming those timestamps are at the beginning of the line:
awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'
Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.
Another approach to work around that would be:
awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
$0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'
That is use a range like in sed
, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.
Worked like charm. But then I saw that it doesn't include stack traces.
â muhammad
Aug 29 at 11:22
@muhammad, see edit.
â Stéphane Chazelas
Aug 29 at 11:34
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Not so strange. sed
is a stream editor, it processes lines as they come. A range like /a/,/b/
means the lines are selected as soon as a
is found, and no-longer selected after b
is found. If b
is never found, it never stops selecting lines.
Here, you should rather use awk
instead. Assuming those timestamps are at the beginning of the line:
awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'
Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.
Another approach to work around that would be:
awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
$0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'
That is use a range like in sed
, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.
Not so strange. sed
is a stream editor, it processes lines as they come. A range like /a/,/b/
means the lines are selected as soon as a
is found, and no-longer selected after b
is found. If b
is never found, it never stops selecting lines.
Here, you should rather use awk
instead. Assuming those timestamps are at the beginning of the line:
awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'
Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.
Another approach to work around that would be:
awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
$0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'
That is use a range like in sed
, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.
edited Aug 29 at 12:05
answered Aug 29 at 11:19
Stéphane Chazelas
286k53527866
286k53527866
Worked like charm. But then I saw that it doesn't include stack traces.
â muhammad
Aug 29 at 11:22
@muhammad, see edit.
â Stéphane Chazelas
Aug 29 at 11:34
add a comment |Â
Worked like charm. But then I saw that it doesn't include stack traces.
â muhammad
Aug 29 at 11:22
@muhammad, see edit.
â Stéphane Chazelas
Aug 29 at 11:34
Worked like charm. But then I saw that it doesn't include stack traces.
â muhammad
Aug 29 at 11:22
Worked like charm. But then I saw that it doesn't include stack traces.
â muhammad
Aug 29 at 11:22
@muhammad, see edit.
â Stéphane Chazelas
Aug 29 at 11:34
@muhammad, see edit.
â Stéphane Chazelas
Aug 29 at 11:34
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465477%2fsed-range-problem-if-the-last-pattern-is-not-met%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password