如何在linux中获取小于/小于提供日期的数据
我有一个sample.txt文件,文件中的数据如下所示:如何在linux中获取小于/小于提供日期的数据,linux,unix,Linux,Unix,我有一个sample.txt文件,文件中的数据如下所示: LINE 1|2017-02-19|LINE 3|LINE 4| LINE 5 | LINE 6 LINE 1|2017-02-06|LINE 3|LINE 4| LINE 5 | LINE 6 LINE 1|2017-03-06|LINE 3|LINE 4| LINE 5 | LINE 6 LINE 1|2017-02-07|LINE 3|LINE 4| LINE 5 | LINE 6 LINE 1|2017-03-25|LINE 3|
LINE 1|2017-02-19|LINE 3|LINE 4| LINE 5 | LINE 6
LINE 1|2017-02-06|LINE 3|LINE 4| LINE 5 | LINE 6
LINE 1|2017-03-06|LINE 3|LINE 4| LINE 5 | LINE 6
LINE 1|2017-02-07|LINE 3|LINE 4| LINE 5 | LINE 6
LINE 1|2017-03-25|LINE 3|LINE 4| LINE 5 | LINE 6
LINE 1|2017-02-06|LINE 3|LINE 4| LINE 5 | LINE 6
现在我想从sample.txt文件中获取数据,其中
2017-02-19
2017-02-06
2017-03-06
2017-02-07
2017-03-25
2017-02-06
> 2017-02-19
所以我的输出应该是
LINE 1|2017-03-06|LINE 3|LINE 4| LINE 5 | LINE 6
LINE 1|2017-03-25|LINE 3|LINE 4| LINE 5 | LINE 6
使用GNU和
将FS设置为|
日期-d[date]+[OUTPUT_FORMAT]…将字段2$2格式化为%s之后的秒数,并将其传递到变量d
Awk将测试d是否小于2017-02-19,其自历元起的秒数为1487480400,如果为真,将打印整个记录$0
密码
注意:通过更改“打印$0”,您只能打印某些字段;要打印$1;,打印$1、$2;,甚至打印出你想要的任何东西,报价为2美元
查找自给定日期起的秒数
例如,您可以使用命令行中的date-d2017-02-19+%s查找给定日期后的秒数。逐行读取文件, 隔离日期, 比较格式化为数字的日期, 跳过该行,或打印该行
#!/bin/ksh
compareDay=$( date -d '2017-02-19' "+%Y%j" ) # build a number from <year><day of year>
cat sample.txt | while read line; do
day="${line#*\|}" # remove from the beginning to first |
day="${day%%\|*}" # remove from the end (%%=all) up from |
# compare, skip if condition not met
[[ $( date -d $day "+%Y%j" ) -gt $compareDay ]] || continue
echo "$line" # output the remaining line meeting the condition
done
可能类似于awk'$0>第1行| 2017-02-20'sample.txt
#!/bin/ksh
compareDay=$( date -d '2017-02-19' "+%Y%j" ) # build a number from <year><day of year>
cat sample.txt | while read line; do
day="${line#*\|}" # remove from the beginning to first |
day="${day%%\|*}" # remove from the end (%%=all) up from |
# compare, skip if condition not met
[[ $( date -d $day "+%Y%j" ) -gt $compareDay ]] || continue
echo "$line" # output the remaining line meeting the condition
done
cat sample.txt | while read line; do
[[ $line > 'LINE 1|2017-02-20' ]] || continue
echo "$line"
done