在 awk 中使用带冒号的 shell 脚本参数
我有一个文件,其中包含具有以下格式的行:
2011/09/14 12:00:23.525 text
我编写了一个 shell 脚本,它在 text 中搜索给定的表达式,并按日期和时间对所有匹配的行进行排序。最后,我想丢弃引用比作为参数传递的时刻更旧的时刻的条目。我使用 awk 来进行这种过滤。该脚本如下所示:
search=$1 file=$2 day=$3 time=$4
zgrep -h "$search" $file | sort -k1,1 -k2,2 | awk -v da="$day" ti="$time" '($1 >= day) && ($2>= ti) {print $0}' > out.$$
如果我调用:
myScript searchThis file1.txt 2011/09/20 09:16:52.130
我收到此错误:
awk: ti=09:16:52.130
awk: ^ syntax error
你能帮我解决这个问题吗? 多谢! 再见
I have a file which contains rows having this format:
2011/09/14 12:00:23.525 text
I wrote a shell script which searches for a given expression in text and sorts all the matching rows by day and time. At the end, I would like to discard the entries referring to an instant which is older than that passed as parameter. I use awk to make this kind of filtering. The script looks like this:
search=$1 file=$2 day=$3 time=$4
zgrep -h "$search" $file | sort -k1,1 -k2,2 | awk -v da="$day" ti="$time" '($1 >= day) && ($2>= ti) {print $0}' > out.$
If I invoke:
myScript searchThis file1.txt 2011/09/20 09:16:52.130
I get this error:
awk: ti=09:16:52.130
awk: ^ syntax error
Can you please help me to solve this?
Thanks a lot!
Bye
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能把 para 值包装在
"
中吗?我在 bash 和 zsh 下测试了这个,都有效:
不知道是否有帮助......
can you wrap the para value in
"
?I tested this under bash and zsh, both worked:
don't know if it helps...
肯特的回答对我不起作用。我在 cygwin 中使用 GNU Awk 4.1.3。
我最终发现了这个 awk: Moving Variables from bash 其中 dubiousjim 提到你可以指定操作后的变量值。它在 BEGIN 规则中不可用,但这对我来说很好。
因此,您可以将脚本更改为:
Kent's answer didn't work for me. I'm using GNU Awk 4.1.3 in cygwin.
I eventually found this awk: passing variables from bash where dubiousjim mentioned that you can specify the variable value after the action. It won't be available in the BEGIN rule but that was fine for my case.
So you can change your script to something like: