Use Sed Regex Capture Group in Replace Section Method
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a list of timestamped filenames in this following format:
cat files
- ..201807010112.log
- ..201807020112.log
- ..201807022359.log
- ..201807030112.log
- ..201807010412.log
I need to get a certain range and in order to do this, I'd like to use SED.
sed -n '/201807010112/,/201807030112/p'
Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.
I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.
cat files | sed -e "s/([0-9]12}/$(date -f '%Y%m%d%H%M' 1)/g"
My problem is that I cannot use the result of the match 1 in the date conversion block.
Is there a way to do this or better yet a better way to get the range of dates?
linux awk sed grep regular-expression
 |Â
show 1 more comment
up vote
2
down vote
favorite
I have a list of timestamped filenames in this following format:
cat files
- ..201807010112.log
- ..201807020112.log
- ..201807022359.log
- ..201807030112.log
- ..201807010412.log
I need to get a certain range and in order to do this, I'd like to use SED.
sed -n '/201807010112/,/201807030112/p'
Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.
I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.
cat files | sed -e "s/([0-9]12}/$(date -f '%Y%m%d%H%M' 1)/g"
My problem is that I cannot use the result of the match 1 in the date conversion block.
Is there a way to do this or better yet a better way to get the range of dates?
linux awk sed grep regular-expression
3
This is a very good example of a XY question...
â don_crissti
Aug 6 at 22:23
cat 2018070[1-2],301
â user1133275
Aug 6 at 22:49
1
like... sycamore?
â mikeserv
Aug 7 at 0:52
1
Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
â Proximo
Aug 7 at 1:47
Where is yourfiles
contents coming from? There might be a better way to get the filenames than from that file, e.g. usingfind
against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
â Kusalananda
Aug 7 at 6:14
 |Â
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a list of timestamped filenames in this following format:
cat files
- ..201807010112.log
- ..201807020112.log
- ..201807022359.log
- ..201807030112.log
- ..201807010412.log
I need to get a certain range and in order to do this, I'd like to use SED.
sed -n '/201807010112/,/201807030112/p'
Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.
I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.
cat files | sed -e "s/([0-9]12}/$(date -f '%Y%m%d%H%M' 1)/g"
My problem is that I cannot use the result of the match 1 in the date conversion block.
Is there a way to do this or better yet a better way to get the range of dates?
linux awk sed grep regular-expression
I have a list of timestamped filenames in this following format:
cat files
- ..201807010112.log
- ..201807020112.log
- ..201807022359.log
- ..201807030112.log
- ..201807010412.log
I need to get a certain range and in order to do this, I'd like to use SED.
sed -n '/201807010112/,/201807030112/p'
Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.
I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.
cat files | sed -e "s/([0-9]12}/$(date -f '%Y%m%d%H%M' 1)/g"
My problem is that I cannot use the result of the match 1 in the date conversion block.
Is there a way to do this or better yet a better way to get the range of dates?
linux awk sed grep regular-expression
linux awk sed grep regular-expression
edited Aug 7 at 1:23
asked Aug 6 at 21:55
Proximo
1235
1235
3
This is a very good example of a XY question...
â don_crissti
Aug 6 at 22:23
cat 2018070[1-2],301
â user1133275
Aug 6 at 22:49
1
like... sycamore?
â mikeserv
Aug 7 at 0:52
1
Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
â Proximo
Aug 7 at 1:47
Where is yourfiles
contents coming from? There might be a better way to get the filenames than from that file, e.g. usingfind
against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
â Kusalananda
Aug 7 at 6:14
 |Â
show 1 more comment
3
This is a very good example of a XY question...
â don_crissti
Aug 6 at 22:23
cat 2018070[1-2],301
â user1133275
Aug 6 at 22:49
1
like... sycamore?
â mikeserv
Aug 7 at 0:52
1
Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
â Proximo
Aug 7 at 1:47
Where is yourfiles
contents coming from? There might be a better way to get the filenames than from that file, e.g. usingfind
against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
â Kusalananda
Aug 7 at 6:14
3
3
This is a very good example of a XY question...
â don_crissti
Aug 6 at 22:23
This is a very good example of a XY question...
â don_crissti
Aug 6 at 22:23
cat 2018070[1-2],301
â user1133275
Aug 6 at 22:49
cat 2018070[1-2],301
â user1133275
Aug 6 at 22:49
1
1
like... sycamore?
â mikeserv
Aug 7 at 0:52
like... sycamore?
â mikeserv
Aug 7 at 0:52
1
1
Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
â Proximo
Aug 7 at 1:47
Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
â Proximo
Aug 7 at 1:47
Where is your
files
contents coming from? There might be a better way to get the filenames than from that file, e.g. using find
against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.â Kusalananda
Aug 7 at 6:14
Where is your
files
contents coming from? There might be a better way to get the filenames than from that file, e.g. using find
against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.â Kusalananda
Aug 7 at 6:14
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
So I decided not to try to use date function to transform the form and instead use more SED!
cat files
..._201807010112.log
..._201807010132.log
..._201807010152.log
..._201807010202.log
The date is in the format YYYYmmddHHSS and I transformed it to the following format:
YYYY-mm-dd HH:SS using this regex expression with SED.
cat files | sed -e 's/(_[0-9]4)([0-9]2)
([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'
Next, from there it was a piece of cake to specify the range based on date.
| sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'
The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
So I decided not to try to use date function to transform the form and instead use more SED!
cat files
..._201807010112.log
..._201807010132.log
..._201807010152.log
..._201807010202.log
The date is in the format YYYYmmddHHSS and I transformed it to the following format:
YYYY-mm-dd HH:SS using this regex expression with SED.
cat files | sed -e 's/(_[0-9]4)([0-9]2)
([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'
Next, from there it was a piece of cake to specify the range based on date.
| sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'
The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.
add a comment |Â
up vote
0
down vote
So I decided not to try to use date function to transform the form and instead use more SED!
cat files
..._201807010112.log
..._201807010132.log
..._201807010152.log
..._201807010202.log
The date is in the format YYYYmmddHHSS and I transformed it to the following format:
YYYY-mm-dd HH:SS using this regex expression with SED.
cat files | sed -e 's/(_[0-9]4)([0-9]2)
([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'
Next, from there it was a piece of cake to specify the range based on date.
| sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'
The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
So I decided not to try to use date function to transform the form and instead use more SED!
cat files
..._201807010112.log
..._201807010132.log
..._201807010152.log
..._201807010202.log
The date is in the format YYYYmmddHHSS and I transformed it to the following format:
YYYY-mm-dd HH:SS using this regex expression with SED.
cat files | sed -e 's/(_[0-9]4)([0-9]2)
([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'
Next, from there it was a piece of cake to specify the range based on date.
| sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'
The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.
So I decided not to try to use date function to transform the form and instead use more SED!
cat files
..._201807010112.log
..._201807010132.log
..._201807010152.log
..._201807010202.log
The date is in the format YYYYmmddHHSS and I transformed it to the following format:
YYYY-mm-dd HH:SS using this regex expression with SED.
cat files | sed -e 's/(_[0-9]4)([0-9]2)
([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'
Next, from there it was a piece of cake to specify the range based on date.
| sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'
The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.
answered Aug 7 at 1:59
Proximo
1235
1235
add a comment |Â
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%2f460912%2fuse-sed-regex-capture-group-in-replace-section-method%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
3
This is a very good example of a XY question...
â don_crissti
Aug 6 at 22:23
cat 2018070[1-2],301
â user1133275
Aug 6 at 22:49
1
like... sycamore?
â mikeserv
Aug 7 at 0:52
1
Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
â Proximo
Aug 7 at 1:47
Where is your
files
contents coming from? There might be a better way to get the filenames than from that file, e.g. usingfind
against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.â Kusalananda
Aug 7 at 6:14