pass arguments to the date command in LHS of sed

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I need to convert a date string from the form: "Jan 07, 2016 12:12:12 EDT" (from a nessus report) to a simple 2016-01-07. I have the match logic solved in sed (and python and awk) and the conversion logic also solved (outside of sed) with the use of the date command: date "Jan 07, 2016 12:12:12 EDT" +%Y-%m-%d
I am using match groups to capture the month, day and year so I can refer to them in back references. I just cannot seem to find the correct combination of quotes to get the data command to recognize the arguments in the RHS of a sed match, e.g.:
sed -E "s/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(s[0-9]1,2[,].[0-9]1,4.?[0-9]1,2.[0-9]1,2.[0-9]1,2.w3)/`date -d "12" "+%Y-%m-%d"`/g" 20170917.csv
Note using any quoting, results in date ignoring the first argument and printing today's date according to the second argument.
I have iterated through using ",',",' to surround the backrefs 1 and 2 but it seems that as soon as they are enclosed in back ticks, `` to allow the date command to be executed, they are ignored.
Input is csv from Tenable Nessus report.
head tmp/20180121.csv
(1) "Plugin","Plugin Name","Family","Severity","IP Address","Protocol","Port","Exploit?","Repository","MAC Address","DNS Name","NetBIOS Name","Plugin Text","First Discovered","Last Observed","Mitigated On","Exploit Frameworks"
(2) "73571","Oracle Java SE Multiple Vulnerabilities (April 2014 CPU) (Unix)","Misc.","Critical","10.140.162.132","TCP","0","Yes","Individual Scan","00:50:56:c0:00:01","host.example.com","","Plugin Output:
The following vulnerable instance of Java is installed on the
remote host :
Path : /opt/Geneious_linux64_7_1_9_with_jre/
Installed version : 1.7.0_51
Fixed version : 1.5.0_65 / 1.6.0_75 / 1.7.0_55 / 1.8.0_5","Jan 21, 2018 22:14:50 EST","Jan 21, 2018 22:14:50 EST","",""
Each numbered line is a record -- (1) is the header, and (2) is representative of the remainder.
The Date strings to be converted are in the last line of row 2. I need this be simply: 2018-01-21 in both cases.
sed
add a comment |Â
up vote
0
down vote
favorite
I need to convert a date string from the form: "Jan 07, 2016 12:12:12 EDT" (from a nessus report) to a simple 2016-01-07. I have the match logic solved in sed (and python and awk) and the conversion logic also solved (outside of sed) with the use of the date command: date "Jan 07, 2016 12:12:12 EDT" +%Y-%m-%d
I am using match groups to capture the month, day and year so I can refer to them in back references. I just cannot seem to find the correct combination of quotes to get the data command to recognize the arguments in the RHS of a sed match, e.g.:
sed -E "s/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(s[0-9]1,2[,].[0-9]1,4.?[0-9]1,2.[0-9]1,2.[0-9]1,2.w3)/`date -d "12" "+%Y-%m-%d"`/g" 20170917.csv
Note using any quoting, results in date ignoring the first argument and printing today's date according to the second argument.
I have iterated through using ",',",' to surround the backrefs 1 and 2 but it seems that as soon as they are enclosed in back ticks, `` to allow the date command to be executed, they are ignored.
Input is csv from Tenable Nessus report.
head tmp/20180121.csv
(1) "Plugin","Plugin Name","Family","Severity","IP Address","Protocol","Port","Exploit?","Repository","MAC Address","DNS Name","NetBIOS Name","Plugin Text","First Discovered","Last Observed","Mitigated On","Exploit Frameworks"
(2) "73571","Oracle Java SE Multiple Vulnerabilities (April 2014 CPU) (Unix)","Misc.","Critical","10.140.162.132","TCP","0","Yes","Individual Scan","00:50:56:c0:00:01","host.example.com","","Plugin Output:
The following vulnerable instance of Java is installed on the
remote host :
Path : /opt/Geneious_linux64_7_1_9_with_jre/
Installed version : 1.7.0_51
Fixed version : 1.5.0_65 / 1.6.0_75 / 1.7.0_55 / 1.8.0_5","Jan 21, 2018 22:14:50 EST","Jan 21, 2018 22:14:50 EST","",""
Each numbered line is a record -- (1) is the header, and (2) is representative of the remainder.
The Date strings to be converted are in the last line of row 2. I need this be simply: 2018-01-21 in both cases.
sed
1. Onlygnu sedcan do this via theeflag. 2. Even then, you're using the wrong tool for this job:awk,perl,pythonetc are much better tools for this task...
â don_crissti
Sep 7 at 20:22
Or use dateutils, specificallydconv.
â Wildcard
Sep 7 at 20:23
1
... and btw, that's the RHS not the LHS...
â don_crissti
Sep 7 at 20:28
Roger, RHS, I meant that... ;)
â Rohn Wood
Sep 7 at 20:35
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to convert a date string from the form: "Jan 07, 2016 12:12:12 EDT" (from a nessus report) to a simple 2016-01-07. I have the match logic solved in sed (and python and awk) and the conversion logic also solved (outside of sed) with the use of the date command: date "Jan 07, 2016 12:12:12 EDT" +%Y-%m-%d
I am using match groups to capture the month, day and year so I can refer to them in back references. I just cannot seem to find the correct combination of quotes to get the data command to recognize the arguments in the RHS of a sed match, e.g.:
sed -E "s/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(s[0-9]1,2[,].[0-9]1,4.?[0-9]1,2.[0-9]1,2.[0-9]1,2.w3)/`date -d "12" "+%Y-%m-%d"`/g" 20170917.csv
Note using any quoting, results in date ignoring the first argument and printing today's date according to the second argument.
I have iterated through using ",',",' to surround the backrefs 1 and 2 but it seems that as soon as they are enclosed in back ticks, `` to allow the date command to be executed, they are ignored.
Input is csv from Tenable Nessus report.
head tmp/20180121.csv
(1) "Plugin","Plugin Name","Family","Severity","IP Address","Protocol","Port","Exploit?","Repository","MAC Address","DNS Name","NetBIOS Name","Plugin Text","First Discovered","Last Observed","Mitigated On","Exploit Frameworks"
(2) "73571","Oracle Java SE Multiple Vulnerabilities (April 2014 CPU) (Unix)","Misc.","Critical","10.140.162.132","TCP","0","Yes","Individual Scan","00:50:56:c0:00:01","host.example.com","","Plugin Output:
The following vulnerable instance of Java is installed on the
remote host :
Path : /opt/Geneious_linux64_7_1_9_with_jre/
Installed version : 1.7.0_51
Fixed version : 1.5.0_65 / 1.6.0_75 / 1.7.0_55 / 1.8.0_5","Jan 21, 2018 22:14:50 EST","Jan 21, 2018 22:14:50 EST","",""
Each numbered line is a record -- (1) is the header, and (2) is representative of the remainder.
The Date strings to be converted are in the last line of row 2. I need this be simply: 2018-01-21 in both cases.
sed
I need to convert a date string from the form: "Jan 07, 2016 12:12:12 EDT" (from a nessus report) to a simple 2016-01-07. I have the match logic solved in sed (and python and awk) and the conversion logic also solved (outside of sed) with the use of the date command: date "Jan 07, 2016 12:12:12 EDT" +%Y-%m-%d
I am using match groups to capture the month, day and year so I can refer to them in back references. I just cannot seem to find the correct combination of quotes to get the data command to recognize the arguments in the RHS of a sed match, e.g.:
sed -E "s/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(s[0-9]1,2[,].[0-9]1,4.?[0-9]1,2.[0-9]1,2.[0-9]1,2.w3)/`date -d "12" "+%Y-%m-%d"`/g" 20170917.csv
Note using any quoting, results in date ignoring the first argument and printing today's date according to the second argument.
I have iterated through using ",',",' to surround the backrefs 1 and 2 but it seems that as soon as they are enclosed in back ticks, `` to allow the date command to be executed, they are ignored.
Input is csv from Tenable Nessus report.
head tmp/20180121.csv
(1) "Plugin","Plugin Name","Family","Severity","IP Address","Protocol","Port","Exploit?","Repository","MAC Address","DNS Name","NetBIOS Name","Plugin Text","First Discovered","Last Observed","Mitigated On","Exploit Frameworks"
(2) "73571","Oracle Java SE Multiple Vulnerabilities (April 2014 CPU) (Unix)","Misc.","Critical","10.140.162.132","TCP","0","Yes","Individual Scan","00:50:56:c0:00:01","host.example.com","","Plugin Output:
The following vulnerable instance of Java is installed on the
remote host :
Path : /opt/Geneious_linux64_7_1_9_with_jre/
Installed version : 1.7.0_51
Fixed version : 1.5.0_65 / 1.6.0_75 / 1.7.0_55 / 1.8.0_5","Jan 21, 2018 22:14:50 EST","Jan 21, 2018 22:14:50 EST","",""
Each numbered line is a record -- (1) is the header, and (2) is representative of the remainder.
The Date strings to be converted are in the last line of row 2. I need this be simply: 2018-01-21 in both cases.
sed
sed
edited Sep 7 at 22:42
asked Sep 7 at 20:16
Rohn Wood
92
92
1. Onlygnu sedcan do this via theeflag. 2. Even then, you're using the wrong tool for this job:awk,perl,pythonetc are much better tools for this task...
â don_crissti
Sep 7 at 20:22
Or use dateutils, specificallydconv.
â Wildcard
Sep 7 at 20:23
1
... and btw, that's the RHS not the LHS...
â don_crissti
Sep 7 at 20:28
Roger, RHS, I meant that... ;)
â Rohn Wood
Sep 7 at 20:35
add a comment |Â
1. Onlygnu sedcan do this via theeflag. 2. Even then, you're using the wrong tool for this job:awk,perl,pythonetc are much better tools for this task...
â don_crissti
Sep 7 at 20:22
Or use dateutils, specificallydconv.
â Wildcard
Sep 7 at 20:23
1
... and btw, that's the RHS not the LHS...
â don_crissti
Sep 7 at 20:28
Roger, RHS, I meant that... ;)
â Rohn Wood
Sep 7 at 20:35
1. Only
gnu sed can do this via the e flag. 2. Even then, you're using the wrong tool for this job: awk, perl, python etc are much better tools for this task...â don_crissti
Sep 7 at 20:22
1. Only
gnu sed can do this via the e flag. 2. Even then, you're using the wrong tool for this job: awk, perl, python etc are much better tools for this task...â don_crissti
Sep 7 at 20:22
Or use dateutils, specifically
dconv.â Wildcard
Sep 7 at 20:23
Or use dateutils, specifically
dconv.â Wildcard
Sep 7 at 20:23
1
1
... and btw, that's the RHS not the LHS...
â don_crissti
Sep 7 at 20:28
... and btw, that's the RHS not the LHS...
â don_crissti
Sep 7 at 20:28
Roger, RHS, I meant that... ;)
â Rohn Wood
Sep 7 at 20:35
Roger, RHS, I meant that... ;)
â Rohn Wood
Sep 7 at 20:35
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
A common way to do it is to add a translation table and use back-references:
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/$/Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12/
s/^(...) (..), (....).*1(..).*/3-2-4/'
If you wanted to have sed run date to translate the date, you'd have to use a GNU extension: the e flag of the s command (and also the GNU date extension -d).
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/^([[:alpha:]]3 [[:digit:]]2, [[:digit:]]4).*$/date -d "1" +%F/e'
Though than means running a shell and date for each line of input, which is pretty inefficient (and also dangerous as we're having sh interpret code that is dynamically generated based on input which we don't know whether we can trust or not). You might as well use a text processor that can do the translation by itself like perl, if you don't want to implement it in sed.
Good advise all around. Also, much cleaner regex. My failure for not reading closer into sed to discover the 'e' flag. I think we can close this off. I appreciate everyone's attention and responses.
â Rohn Wood
Sep 7 at 22:46
@RohnWood, note that StackExchange is more like a wiki than a support request site. We don't close off requests there. As the original poster, you can accept one of the answers (using the tick box) which indicates that you are happy with the answer given and think it's the best for your need, but that doesn't stop people from suggesting other answers which may be useful to you or other people with similar needs. Questions may be closed through a voting system if they're deemed inappropriate by the community but that's not the case here.
â Stéphane Chazelas
Sep 7 at 23:20
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
A common way to do it is to add a translation table and use back-references:
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/$/Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12/
s/^(...) (..), (....).*1(..).*/3-2-4/'
If you wanted to have sed run date to translate the date, you'd have to use a GNU extension: the e flag of the s command (and also the GNU date extension -d).
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/^([[:alpha:]]3 [[:digit:]]2, [[:digit:]]4).*$/date -d "1" +%F/e'
Though than means running a shell and date for each line of input, which is pretty inefficient (and also dangerous as we're having sh interpret code that is dynamically generated based on input which we don't know whether we can trust or not). You might as well use a text processor that can do the translation by itself like perl, if you don't want to implement it in sed.
Good advise all around. Also, much cleaner regex. My failure for not reading closer into sed to discover the 'e' flag. I think we can close this off. I appreciate everyone's attention and responses.
â Rohn Wood
Sep 7 at 22:46
@RohnWood, note that StackExchange is more like a wiki than a support request site. We don't close off requests there. As the original poster, you can accept one of the answers (using the tick box) which indicates that you are happy with the answer given and think it's the best for your need, but that doesn't stop people from suggesting other answers which may be useful to you or other people with similar needs. Questions may be closed through a voting system if they're deemed inappropriate by the community but that's not the case here.
â Stéphane Chazelas
Sep 7 at 23:20
add a comment |Â
up vote
0
down vote
A common way to do it is to add a translation table and use back-references:
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/$/Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12/
s/^(...) (..), (....).*1(..).*/3-2-4/'
If you wanted to have sed run date to translate the date, you'd have to use a GNU extension: the e flag of the s command (and also the GNU date extension -d).
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/^([[:alpha:]]3 [[:digit:]]2, [[:digit:]]4).*$/date -d "1" +%F/e'
Though than means running a shell and date for each line of input, which is pretty inefficient (and also dangerous as we're having sh interpret code that is dynamically generated based on input which we don't know whether we can trust or not). You might as well use a text processor that can do the translation by itself like perl, if you don't want to implement it in sed.
Good advise all around. Also, much cleaner regex. My failure for not reading closer into sed to discover the 'e' flag. I think we can close this off. I appreciate everyone's attention and responses.
â Rohn Wood
Sep 7 at 22:46
@RohnWood, note that StackExchange is more like a wiki than a support request site. We don't close off requests there. As the original poster, you can accept one of the answers (using the tick box) which indicates that you are happy with the answer given and think it's the best for your need, but that doesn't stop people from suggesting other answers which may be useful to you or other people with similar needs. Questions may be closed through a voting system if they're deemed inappropriate by the community but that's not the case here.
â Stéphane Chazelas
Sep 7 at 23:20
add a comment |Â
up vote
0
down vote
up vote
0
down vote
A common way to do it is to add a translation table and use back-references:
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/$/Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12/
s/^(...) (..), (....).*1(..).*/3-2-4/'
If you wanted to have sed run date to translate the date, you'd have to use a GNU extension: the e flag of the s command (and also the GNU date extension -d).
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/^([[:alpha:]]3 [[:digit:]]2, [[:digit:]]4).*$/date -d "1" +%F/e'
Though than means running a shell and date for each line of input, which is pretty inefficient (and also dangerous as we're having sh interpret code that is dynamically generated based on input which we don't know whether we can trust or not). You might as well use a text processor that can do the translation by itself like perl, if you don't want to implement it in sed.
A common way to do it is to add a translation table and use back-references:
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/$/Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12/
s/^(...) (..), (....).*1(..).*/3-2-4/'
If you wanted to have sed run date to translate the date, you'd have to use a GNU extension: the e flag of the s command (and also the GNU date extension -d).
echo 'Jan 07, 2016 12:12:12 EDT' | sed -E '
s/^([[:alpha:]]3 [[:digit:]]2, [[:digit:]]4).*$/date -d "1" +%F/e'
Though than means running a shell and date for each line of input, which is pretty inefficient (and also dangerous as we're having sh interpret code that is dynamically generated based on input which we don't know whether we can trust or not). You might as well use a text processor that can do the translation by itself like perl, if you don't want to implement it in sed.
edited Sep 7 at 22:17
answered Sep 7 at 22:08
Stéphane Chazelas
286k53528866
286k53528866
Good advise all around. Also, much cleaner regex. My failure for not reading closer into sed to discover the 'e' flag. I think we can close this off. I appreciate everyone's attention and responses.
â Rohn Wood
Sep 7 at 22:46
@RohnWood, note that StackExchange is more like a wiki than a support request site. We don't close off requests there. As the original poster, you can accept one of the answers (using the tick box) which indicates that you are happy with the answer given and think it's the best for your need, but that doesn't stop people from suggesting other answers which may be useful to you or other people with similar needs. Questions may be closed through a voting system if they're deemed inappropriate by the community but that's not the case here.
â Stéphane Chazelas
Sep 7 at 23:20
add a comment |Â
Good advise all around. Also, much cleaner regex. My failure for not reading closer into sed to discover the 'e' flag. I think we can close this off. I appreciate everyone's attention and responses.
â Rohn Wood
Sep 7 at 22:46
@RohnWood, note that StackExchange is more like a wiki than a support request site. We don't close off requests there. As the original poster, you can accept one of the answers (using the tick box) which indicates that you are happy with the answer given and think it's the best for your need, but that doesn't stop people from suggesting other answers which may be useful to you or other people with similar needs. Questions may be closed through a voting system if they're deemed inappropriate by the community but that's not the case here.
â Stéphane Chazelas
Sep 7 at 23:20
Good advise all around. Also, much cleaner regex. My failure for not reading closer into sed to discover the 'e' flag. I think we can close this off. I appreciate everyone's attention and responses.
â Rohn Wood
Sep 7 at 22:46
Good advise all around. Also, much cleaner regex. My failure for not reading closer into sed to discover the 'e' flag. I think we can close this off. I appreciate everyone's attention and responses.
â Rohn Wood
Sep 7 at 22:46
@RohnWood, note that StackExchange is more like a wiki than a support request site. We don't close off requests there. As the original poster, you can accept one of the answers (using the tick box) which indicates that you are happy with the answer given and think it's the best for your need, but that doesn't stop people from suggesting other answers which may be useful to you or other people with similar needs. Questions may be closed through a voting system if they're deemed inappropriate by the community but that's not the case here.
â Stéphane Chazelas
Sep 7 at 23:20
@RohnWood, note that StackExchange is more like a wiki than a support request site. We don't close off requests there. As the original poster, you can accept one of the answers (using the tick box) which indicates that you are happy with the answer given and think it's the best for your need, but that doesn't stop people from suggesting other answers which may be useful to you or other people with similar needs. Questions may be closed through a voting system if they're deemed inappropriate by the community but that's not the case here.
â Stéphane Chazelas
Sep 7 at 23:20
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%2f467617%2fpass-arguments-to-the-date-command-in-lhs-of-sed%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
1. Only
gnu sedcan do this via theeflag. 2. Even then, you're using the wrong tool for this job:awk,perl,pythonetc are much better tools for this task...â don_crissti
Sep 7 at 20:22
Or use dateutils, specifically
dconv.â Wildcard
Sep 7 at 20:23
1
... and btw, that's the RHS not the LHS...
â don_crissti
Sep 7 at 20:28
Roger, RHS, I meant that... ;)
â Rohn Wood
Sep 7 at 20:35