【发布时间】:2018-09-19 09:16:10
【问题描述】:
我的服务器中有这个文件,我需要使用指定日期获取特定范围的行,所以我搜索了一个脚本并想出了这个。
19 Sep 2018 08:47:57,153#ad****#L****#2018-Sep-19 08:47:43 110#2018-Sep-19
08:47:57 153#14.***sec
19 Sep 2018 09:00:25,582#jg****#L****#2018-Sep-19 09:00:22 658#2018-Sep-19
09:00:25 582#2.***sec
19 Sep 2018 09:00:25,720#lr****#L****#2018-Sep-19 09:00:22 038#2018-Sep-19
09:00:25 720#3.***sec
....
我在命令行输入了这个命令,效果很好:awk '$0 >= "19 Sep 2018 09:00:00" && $0 <= "19 Sep 2018 09:05:00"
'/home/../k../g../somelogfile.txt
但是当我在脚本中操作它并将变量放入其中时,没有输出。
todayl=$(date --date="19 Sept 2018 09:00:00AM")
t_i_f_o=$(date +"%d %b %Y %H:%M:%S" --date="$today")
t_f_m_a=$(date +"%d %b %Y %H:%M:%S" --date="$today + 5 minutes")
awk '$0 >= "$t_i_f_o" && $0 <= "$t_f_m_a" ' /home/m.../k.../t.../somelogfile1.txt > /home/m.../k.../t../f.../another1.txt #Tried this
awk '$0 >= "${t_i_f_o}" && $0 <= "${t_f_m_a}"' /home/m.../k.../t.../somefile2.txt > /home/m.../k.../t.../f.../another2.txt # and this
当我查看 another1.txt 和 another2.txt 的输出文件时,没有数据。我放置变量的方式有问题吗?
【问题讨论】:
-
哦,对不起,我的意思是“约会”
-
我只是想知道为什么当我手动将日期放入 awk 命令时效果很好。我认为我在命令本身中传递变量的方式有问题。
-
了解 shell 扩展 here。将变量放入
'qoutes 不会扩展它们。您需要将变量放在"qoutes 中,其余的都放在'qoutes 中。