无法从shell脚本中的awk命令获取任何输出[duplicate]

时间:2022-03-05 23:49:13

This question already has an answer here:

这个问题已经有了答案:

I am trying to write a shell script to get certain data from below sample logs..

我正在尝试编写一个shell脚本,从下面的示例日志中获取某些数据。

Below is a sample log:

下面是一个样本日志:

2014-07-08 16:08:25,684:        |ABC_130|1|10123ffffff2|P|489440201
2014-07-08 17:08:25,684:        |ABC_130|1|aaaaaxxxxaab|P|489440201
2014-07-08 19:08:25,684:        |ABC_130|1|aaaaababbaab|P|489440201

Below is a part of the script where I am facing issue, the issue I am facing is that the awk command doesn't give any output.

下面是我面临问题的脚本的一部分,我面临的问题是awk命令不提供任何输出。

#!/bin/sh

DATE_HOUR="`date -d '1 hour ago' "+%Y-%m-%d %H"`"

awk -F ":" '{if ($1='"$DATE_HOUR"') print $0}' log.txt 

1 个解决方案

#1


3  

Don't use shell variable like that in awk. Use -v name=val:

不要在awk中使用shell变量。使用- v名称= val:

awk -F ":" -v dt="$DATE_HOUR" '$1==dt' log.txt 

btw I reduced your awk command to '$1==dt' since print $0 is default action and also if condition can be moved out of curly braces.

顺便说一下,我将awk命令减少到“$1== =dt”,因为打印$0是默认操作,如果条件可以从花括号中移出的话。

#1


3  

Don't use shell variable like that in awk. Use -v name=val:

不要在awk中使用shell变量。使用- v名称= val:

awk -F ":" -v dt="$DATE_HOUR" '$1==dt' log.txt 

btw I reduced your awk command to '$1==dt' since print $0 is default action and also if condition can be moved out of curly braces.

顺便说一下,我将awk命令减少到“$1== =dt”,因为打印$0是默认操作,如果条件可以从花括号中移出的话。