简体   繁体   English

在 awk 命令 shell 中使用日期

[英]Using date in awk command shell

I have a question related to awk command in shell.我有一个与 shell 中的 awk 命令有关的问题。 We have a input csv file with following values:我们有一个输入 csv 文件,其值如下:

Control_Time;Report_Name
2020-08-10;Report A
2020-06-10;Report B
2020-07-01;Report C

My goal is to check all the rows in this file and filter (copy) only the rows which have a date greater than first day of previous month.我的目标是检查此文件中的所有行并仅过滤(复制)日期大于上个月第一天的行。 So, I created an variable called previous_month which is following:因此,我创建了一个名为 previous_month 的变量,如下所示:

previous_month=$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)

This variable works fine, but I am not able to work with it in awk statement which I want to use for filtering data into the output file.此变量工作正常,但我无法在 awk 语句中使用它,我想使用该语句将数据过滤到 output 文件中。

I tried following statement just for printing the previous_month and it is not working.我尝试以下语句仅用于打印 previous_month 并且它不起作用。

awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}'

However, echo previous_month works fine and it gives me the value 2020-07-01.但是,echo previous_month 工作正常,它给了我 2020-07-01 的值。 Is there any way how to work with this custom variable?有什么方法可以使用这个自定义变量吗? Thank you for all advices!感谢您的所有建议!

You were close, could you please try following.你很接近,你能试试跟随吗?

awk -v pre_date=$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d) '
BEGIN{
  FS=OFS=";"
}
$1==pre_date
'  Input_file

Improvements in OP's code attempts: OP 代码尝试的改进:

  • We could create an awk variable where we could mention date shell command itself, so taken OP's command of date and placed it inside pre_date awk variable.我们可以创建一个awk变量,我们可以在其中提到date shell 命令本身,因此采用 OP 的date命令并将其放在pre_date Z5E4C8DFA9E20567E2655E847F68193B2 变量中。
  • Since OP wants to match previous month's date with Input_file so first we need to set delimiter as ;由于 OP 想要将上个月的日期与 Input_file 匹配,所以首先我们需要将分隔符设置为; as per OP's sample Input_file.根据 OP 的示例 Input_file。
  • Then we need to compare $1 1st field of Input_file with awk variable and check condition if both are same then no action mentioned so by default print of current line will happen.然后我们需要将 Input_file 的第一个字段$1awk变量进行比较,并检查条件是否相同,然后没有提到任何操作,因此默认情况下会打印当前行。

Your command works just fine.你的命令工作得很好。 awk is just working on lines either given by stdin or by a file. awk 只是在标准输入或文件给出的行上工作。 Or what you're typing.或者你正在输入的内容。

Try the following:尝试以下操作:

echo "hello world" | awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}'

This will print the date once, since you provided one line via stdin.这将打印一次日期,因为您通过标准输入提供了一行。

Or或者

awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}' filename

This will print the date for each line in the file.这将打印文件中每一行的日期。

Or或者

awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}'

(and now type something and press enter) (现在输入一些东西并按回车)

This will print the date as well each time you press enter.这将在您每次按 Enter 时打印日期。

thank you both for your answers.谢谢你们的回答。 It was really helpful, I did not realized.这真的很有帮助,我没有意识到。 that I need to use the input file, So, it worked fine.我需要使用输入文件,所以,它工作得很好。 but I had to add input file.但我必须添加输入文件。

Final query is following:最终查询如下:

awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{if ($1 > previous_month) print $0}' inputFile > outputFile;

It could help to someone else.它可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM