awk 中的 shell 变量问题
我有一个在命令行上成功执行的命令:
ls -l | awk '/Sep 26/{split($8,a,":");if(a[1]a[2]>=1045 && a[1]a[2]<=1145)print $9}'
我在下面的 shell 脚本中包含相同的内容:
#!/bin/ksh
date1=$1
date2=$2
time1=$3
time2=$4
ls -l| awk -v d1=${date1} -v d2=${date2} -v t1=${time1} -v t2=${time2} '/d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 && a[1]a[2]<=t2) print $9}'
但这不起作用。请参阅下面
ksh -vx test.sh Sep 26 1045 1145
#!/bin/ksh
date1=$1
+ date1=Sep
date2=$2
+ date2=26
time1=$3
+ time1=1045
time2=$4
+ time2=1145
ls -l| awk -v d1=${date1} -v d2=${date2} -v t1=${time1} -v t2=${time2} '/d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 &&a[1]a[2]<=t2)print $9}'
+ awk -v d1=Sep -v d2=26 -v t1=1045 -v t2=1145 /d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 &&a[1]a[2]<=t2)print $9}
+ ls -l
awk: syntax error near line 1
awk: bailing out near line 1
我正在使用 Solaris 操作系统的执行:我现在已经尝试使用 nawk,但有没有错误,但也没有输出。
pearl[ncm_o11.2_int.@].293> ksh -vx test.sh Sep 26 1045 1145
#!/bin/ksh
date1=$1
+ date1=Sep
date2=$2
+ date2=26
time1=$3
+ time1=1045
time2=$4
+ time2=1145
ls -l| nawk -v d1=${date1} -v d2=${date2} -v t1=${time1} -v t2=${time2} '/d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 &&a[1]a[2]<=t2)print $9}'
+ ls -l
+ nawk -v d1=Sep -v d2=26 -v t1=1045 -v t2=1145 /d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 &&a[1]a[2]<=t2)print $9}
当我在脚本中没有 shell 变量的情况下执行时,它执行得很好。
*注意:*我使用的是 Solaris 操作系统。
我发现问题出在 shell 脚本中的最终框架命令中:
ls -l|nawk -v d1=Sep -v d2=26 -v t1=1045 -v t2=1145 '/d1 d2/{split($8,a,":");if(a[1]a[2] >=t1 && a[1]a[2]<=t2)print $9}'
但我不确定为什么这无法使用 -v 标志给出正确的输出
I have a command which is executed successfully on command line:
ls -l | awk '/Sep 26/{split($8,a,":");if(a[1]a[2]>=1045 && a[1]a[2]<=1145)print $9}'
I am including the same thing in a shell script below:
#!/bin/ksh
date1=$1
date2=$2
time1=$3
time2=$4
ls -l| awk -v d1=${date1} -v d2=${date2} -v t1=${time1} -v t2=${time2} '/d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 && a[1]a[2]<=t2) print $9}'
But this does not work.please see below the execution
ksh -vx test.sh Sep 26 1045 1145
#!/bin/ksh
date1=$1
+ date1=Sep
date2=$2
+ date2=26
time1=$3
+ time1=1045
time2=$4
+ time2=1145
ls -l| awk -v d1=${date1} -v d2=${date2} -v t1=${time1} -v t2=${time2} '/d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 &&a[1]a[2]<=t2)print $9}'
+ awk -v d1=Sep -v d2=26 -v t1=1045 -v t2=1145 /d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 &&a[1]a[2]<=t2)print $9}
+ ls -l
awk: syntax error near line 1
awk: bailing out near line 1
I am using Solaris OS:I have tried with nawk now but there is no error but also there is no output.
pearl[ncm_o11.2_int.@].293> ksh -vx test.sh Sep 26 1045 1145
#!/bin/ksh
date1=$1
+ date1=Sep
date2=$2
+ date2=26
time1=$3
+ time1=1045
time2=$4
+ time2=1145
ls -l| nawk -v d1=${date1} -v d2=${date2} -v t1=${time1} -v t2=${time2} '/d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 &&a[1]a[2]<=t2)print $9}'
+ ls -l
+ nawk -v d1=Sep -v d2=26 -v t1=1045 -v t2=1145 /d1 d2/{split($8,a,":");if(a[1]a[2]>=t1 &&a[1]a[2]<=t2)print $9}
When i am executing with out the shell variables inside the script.its executing perfectly.
*Note:*I am using Solaris OS.
I figured out the problem lies in the final framed command inside shell script:
ls -l|nawk -v d1=Sep -v d2=26 -v t1=1045 -v t2=1145 '/d1 d2/{split($8,a,":");if(a[1]a[2] >=t1 && a[1]a[2]<=t2)print $9}'
But i am not sure why this is failing to give the correct output with -v flags
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如已经建议的,如果您需要动态正则表达式,则需要使用
$0 ~ d1 " " d2
而不是/d1 d2/
。As already suggested, if you need a dynamic regular expression, you need to use
$0 ~ d1 " " d2
instead of/d1 d2/
.哪个操作系统?当您以交互方式运行命令时,awk 可能会指向不同的 awk 实现。例如,如果您使用的是 Solaris,请尝试使用 nawk(或 gawk)而不是 awk 运行脚本嗯>。
Which operating system? It's possible that when you run the command interactively awk points to a different awk implementation. If you're on Solaris, for example, try running your script with nawk (or gawk), instead of awk.