简体   繁体   English

每当日期格式约束与日期格式列不匹配时,如何使用 awk 按日期列过滤 csv 文件?

[英]How to filter csv file by date column using awk whenever date format constraint does not match date format column?

I need to filter a csv file whom 25th column has the following date format "YYYY-MM-DD hh:mm:ss", in order to only display the entire lines containing today's date, and put them in a separate file (hope that's clear).我需要过滤第 25 列具有以下日期格式“YYYY-MM-DD hh:mm:ss”的 csv 文件,以便只显示包含今天日期的整行,并将它们放在一个单独的文件中(希望是清除)。

I went over a few topics and tried to adapt solutions to my script but I can't make it work.我讨论了几个主题并尝试调整我的脚本的解决方案,但我无法让它工作。

So right now, I am back to the following :所以现在,我回到以下几点:

today= date '+%Y-%m-%d'今天=日期'+%Y-%m-%d'

awk -F ";" awk -F ";" '{ if ($25 == '$today') print }' input.csv > today.csv' '{ if ($25 == '$today') 打印 }' input.csv > today.csv'

I am aware that I am asking something stupid like "if YYYY-MM-DD == YYYY-MM-DD hh:mm:ss, then do something"我知道我在问一些愚蠢的事情,比如“如果 YYYY-MM-DD == YYYY-MM-DD hh:mm:ss,那么做点什么”

My question is, is there a way to put a wildcard somewhere in my today's variable, or should I set two different variables like :我的问题是,有没有办法在我今天的变量中放置通配符,或者我应该设置两个不同的变量,例如:

today_start = date '+%Y-%m-%d' 00:00:00 today_start = 日期 '+%Y-%m-%d' 00:00:00

today_end = date '+%Y-%m-%d' 23:59:59 today_end = 日期 '+%Y-%m-%d' 23:59:59

and then create a condition stating that if column value is between today_start and _today_end, something needs to be done.然后创建一个条件,说明如果列值介于 today_start 和 _today_end 之间,则需要执行某些操作。

Also, I need to keep the format of date column as it is (YYYY-MM-DD hh:mm:ss).另外,我需要保持日期列的格式不变(YYYY-MM-DD hh:mm:ss)。

Thanks a lot in advance, hope someone can help !非常感谢提前,希望有人可以提供帮助!

You can use a regex to match the start of your field, ie match the first 10 characters (YYYY-MM-DD) of the field.您可以使用正则表达式来匹配字段的开头,即匹配字段的前 10 个字符 (YYYY-MM-DD)。

today=$(date '+%Y-%m-%d')
awk -v regex="^$today" -F';' '$25 ~ regex' input.csv > today.csv

This passes the value of the $today variable with -v to awk and prepends a ^ to match the start of the field.这将带有-v$today变量的值传递给awk并在前面添加^以匹配字段的开头。

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

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