KSH:在文件中查找人类日期并将其转换为适当的纪元
KSH: Find a human date in a file and convert it to an epoch in place
我是 KSH 和 grep、awk、sed 等的新手
我的任务是:
我正在编写一个遍历密钥库并打开它们的脚本。然后我想 找到有效截止日期,并将其转换为纪元 。我可以就地执行此操作,或者将 整个文件(包括已编辑和未编辑的片段) 写入一个新文件,如果这样更容易的话。
我的输入是这样的:
Alias name: mycertname
Creation date: Dec 31, 1969
Entry type: entry type
Owner: owner info
Issuer: issuer info
Serial number: serial number
Valid from: 6/11/03 2:23 PM until: 6/6/23 2:23 PM
Certificate fingerprints: <...>
文件中有很多这样的输出。
有一个问题需要考虑。
1) 并非所有这些证书似乎都具有有效起始行。
我已经建立了我计划用来将日期转换为纪元的命令:
date +%s -d"string I cut from input"
我不确定该怎么做,是格式化我的输出。
我理想的输出是这样的:
Alias name: mycertname 1686075780
或
Alias name: mycertname
1686075780
我是这么想的:
awk '/^Alias/ { alias = [=10=] } /^Valid from:/ { sub(/.*until: /, ""); cmd = "date +%s -d \"" [=10=] "\""; if((cmd | getline epoch) != 1) { epoch = "Broken timestamp" } close(cmd); print alias, epoch }' filename
即:
/^Alias/ { # When a line begins with "Alias"
alias = [=11=] # remember it
}
/^Valid from:/ { # When a line begins with "Valid from:"
sub(/.*until: /, "") # Remove everything before the until date
cmd = "date +%s -d \"" [=11=] "\"" # build the shell command to execute
if((cmd | getline epoch) != 1) { # execute it, get its output
epoch = "Broken timestamp" # in case of failure, set easily
# recognizable replacement message.
# If you want to skip broken records like
# those without a timestamp, use "next"
# instead.
}
close(cmd) # close the pipe
print alias, epoch # print the remembered alias followed by
# the output of that command.
}
我是 KSH 和 grep、awk、sed 等的新手
我的任务是:
我正在编写一个遍历密钥库并打开它们的脚本。然后我想 找到有效截止日期,并将其转换为纪元 。我可以就地执行此操作,或者将 整个文件(包括已编辑和未编辑的片段) 写入一个新文件,如果这样更容易的话。
我的输入是这样的:
Alias name: mycertname
Creation date: Dec 31, 1969
Entry type: entry type
Owner: owner info
Issuer: issuer info
Serial number: serial number
Valid from: 6/11/03 2:23 PM until: 6/6/23 2:23 PM
Certificate fingerprints: <...>
文件中有很多这样的输出。
有一个问题需要考虑。
1) 并非所有这些证书似乎都具有有效起始行。
我已经建立了我计划用来将日期转换为纪元的命令:
date +%s -d"string I cut from input"
我不确定该怎么做,是格式化我的输出。
我理想的输出是这样的:
Alias name: mycertname 1686075780
或
Alias name: mycertname
1686075780
我是这么想的:
awk '/^Alias/ { alias = [=10=] } /^Valid from:/ { sub(/.*until: /, ""); cmd = "date +%s -d \"" [=10=] "\""; if((cmd | getline epoch) != 1) { epoch = "Broken timestamp" } close(cmd); print alias, epoch }' filename
即:
/^Alias/ { # When a line begins with "Alias"
alias = [=11=] # remember it
}
/^Valid from:/ { # When a line begins with "Valid from:"
sub(/.*until: /, "") # Remove everything before the until date
cmd = "date +%s -d \"" [=11=] "\"" # build the shell command to execute
if((cmd | getline epoch) != 1) { # execute it, get its output
epoch = "Broken timestamp" # in case of failure, set easily
# recognizable replacement message.
# If you want to skip broken records like
# those without a timestamp, use "next"
# instead.
}
close(cmd) # close the pipe
print alias, epoch # print the remembered alias followed by
# the output of that command.
}