bash 在bash中使用awk比较日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39947847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comparing dates using awk in bash
提问by JimS
So I have a file and each line has some info and a date (birthday). And I want to print the lines with dates after a given date. I use this awk command
所以我有一个文件,每一行都有一些信息和日期(生日)。我想在给定日期之后打印带有日期的行。我使用这个 awk 命令
awk -F '|' 'FNR>1 $dateA<= {print " "" "" "" "" "" "" "}' $FILE
But it doesnt work properly (all file lines are printed). The dates are in YYYY-MM-DD format so alphabetical order is also chronological.
但它不能正常工作(打印所有文件行)。日期采用 YYYY-MM-DD 格式,因此字母顺序也是按时间顺序排列的。
EDIT: Some lines from the input file
编辑:输入文件中的一些行
1099511628908|Chen|Wei|female|1985-08-02|2010-05-24T20:52:26.582+0000|27.98.244.108|Firefox
1099511633435|Smith|Hyman|male|1981-04-19|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer
1099511635042|Kiss|Gyorgy|male|1984-09-14|2010-05-16T22:57:41.808+0000|91.137.244.86|Chrome
1099511635218|Law-Yone|Eric|male|1987-01-20|2010-05-26T20:10:22.515+0000|203.81.95.235|Chrome
1099511638444|Jasani|Chris|female|1981-05-22|2010-04-29T20:50:40.375+0000|196.223.11.62|Firefox
2199023256615|Arbelaez|Gustavo|male|1986-11-02|2010-07-17T18:53:47.633+0000|190.96.218.101|Chrome
回答by Isaac
As it was said by others, a variable in single quotes will not be expanded by the shell. Awk will see the name of the variable, not its value.
正如其他人所说,单引号中的变量不会被 shell 扩展。awk 将看到变量的名称,而不是它的值。
One possible solution is to do this (assuming comparing strings is correct):
一种可能的解决方案是这样做(假设比较字符串是正确的):
dateA='1985-01-01'
infile='file to read values from'
awk -F '|' -v dateA="$dateA" '{if (FNR>1 && dateA<=) {print}}' "$infile"
A more idiomatic solution (a bit less clear is):
一个更惯用的解决方案(不太清楚):
awk 'FNR>1 && dateA<=' FS='|' dateA="$dateA" "$infile"
Or (yes, all the quoting is needed):
或者(是的,需要所有引用):
awk 'FNR>1 && "'"$dateA"'"<=' FS='|' "$infile"
But before even thinking of using this option read this about code injection.
但在考虑使用此选项之前,请阅读有关代码注入的内容。
回答by Gilles Quenot
For this to work :
为此,请执行以下操作:
awk -vdateA=1985-01-01 -F '|' 'FNR>1 dateA<=' $FILE
~~~~~~~~~~~~~~~~~~
回答by Qeole
Your variable $dateA
?is a shell variable. If you embed it between single quotes, it will not be interpreted by the shell (it will not be replaced by its value) and will be forwarded “as is” to awk.
你的变量$dateA
? 是一个 shell 变量。如果将它嵌入在单引号之间,它不会被 shell 解释(它不会被它的值替换)并且会“按原样”转发到 awk。
Awk receives string $dateA
. It believes it is an awk variable, but does not have any value for it, so it uses an empty string instead. All your dates are considered “bigger” to an empty string, so all lines match and are printed.
awk 接收字符串$dateA
。它认为它是一个 awk 变量,但它没有任何值,因此它使用空字符串代替。您的所有日期都被视为“大于”空字符串,因此所有行都匹配并打印出来。
Several solutions to this. Beside Gilles Quenot's one, where you manually indicate to awk that dateA
is a shell variable, you can also take it “out of the quotes” like this:
对此的几种解决方案。除了 Gilles Quenot 的一个,你手动向 awk 表明它dateA
是一个 shell 变量,你也可以像这样把它“从引号中”取出来:
awk -F '|' 'FNR>1 '$dateA'<= {print " "" "" "" "" "" "" "}' $FILE
^ ^
This works when the date is an integer value, but not when it is on YYYY-MM-DD format. In the latter case, we need to… escape the variable with quotes to tell awk this is a string.
这在日期是整数值时有效,但在 YYYY-MM-DD 格式时无效。在后一种情况下,我们需要......用引号转义变量以告诉 awk 这是一个字符串。
We want to do something that looks like "$dateA"
. Since we already added single quotes not to pass $dateA
to awk as is, we must add double quotes around it: "'$dateA'"
. This way, the shell first replaces dateA
with its value, then removes the single quotes, and gives "1985-08-23"
to awk.
我们想做一些看起来像"$dateA"
. 由于我们已经添加了单引号而不是按$dateA
原样传递给 awk,因此我们必须在其周围添加双引号:"'$dateA'"
。这样,shell 首先dateA
用它的值替换,然后删除单引号,并提供"1985-08-23"
给 awk。
So the line is:
所以这条线是:
awk -F '|' 'FNR>1 && "'$dateA'"<= {print " "" "" "" "" "" "" "}' $FILE
Edit: Not sure whether this depends on awk version, but I got a syntax error whith this example and also had to replace FNR>1 '$date'<$5
with FNR>1 && '$date'<$5
(additional &&
). GNU awk here.
编辑:不确定这是否取决于 awk 版本,但我在此示例中遇到了语法错误,并且还必须替换FNR>1 '$date'<$5
为FNR>1 && '$date'<$5
(additional &&
)。GNU awk 在这里。