脚本如下,出错的在while那行。
谢谢。#!/bin/bash
awk '
BEGIN {
FS="[ \t,]+"
while(getline<"file_1" >0) Tstring[$1]++
close("file_1")
}
{
while(getline<"file_2" >0) if( Tstring[$1]>0 ) {print $0 >"add_temp"}#错误就在这里
}
' $
file_1:
aaa,1111111111
bbb,2222222222
ccc,3333333333
把脚本改成file_2:
abc,11
bbb,22
cde,33
脚本运行以后,按回车,就一直在while那行,不结束了。请问是什么问题?#!/bin/bash
awk '
BEGIN {
FS="[ \t,]+"
while(getline<"file_1" >0) Tstring[$1]++
close("file_1")
}
{
while(getline<"file_2" >0) {print $1 } #直观一点,就直接print出来
close("file_2")
}
' $*
其实脚本没啥错的,但是你直接运行这个脚本的话,就相当于运行了一条 awk 命令,然没有指定输入参数.DerekTan 写了:aBiNg, 加了大括号以后脚本是工作了,我刚才说没有用,是因为脚本运行以后,不会自己退出来,我用ctrl+c取消的。后来发现重定向的文件已经生成了。
例子:file_1:
aaa,1111111111
bbb,2222222222
ccc,3333333333
把脚本改成file_2:
abc,11
bbb,22
cde,33
脚本运行以后,按回车,就一直在while那行,不结束了。请问是什么问题?#!/bin/bash
awk '
BEGIN {
FS="[ \t,]+"
while(getline<"file_1" >0) Tstring[$1]++
close("file_1")
}
{
while(getline<"file_2" >0) {print $1 } #直观一点,就直接print出来
close("file_2")
}
' $*
Thanks again:)
非常感谢 bones7456 and aBiNg!bones7456 写了:另外自荐一下我的 awk 手册: http://bones7456.blog.ubuntu.org.cn/200 ... _handbook/
viewtopic.php?t=80721&highlight=
问完就找到答案了,因为if那句本身是在{}里面的,整个是一个action,如果改成下面这样,也是可以的。#!/bin/bash
awk '
BEGIN {
FS="[ \t,]+"
while(getline<"file_1" >0) Tstring[$1]++
close("file_1")
}
{
if( Tstring[$1]>0 ) {print $0 >"add_temp"}#这里为什么必须用if?
}
' $
谢谢楼上两位指点:)#!/bin/bash
awk '
BEGIN {
FS="[ \t,]+"
while(getline<"file_1" >0) Tstring[$1]++
close("file_1")
}
if( Tstring[$1]>0 ) {print $0 >"add_temp"}
' $
那个...我不是什么作者,嘿嘿,只是把简体中文版的格式稍作整理而已....顶多算个译者DerekTan 写了:非常感谢 bones7456 and aBiNg!bones7456 写了:另外自荐一下我的 awk 手册: http://bones7456.blog.ubuntu.org.cn/200 ... _handbook/
viewtopic.php?t=80721&highlight=
bones7456,你的手册写得非常好,我就是先看了你的手册才开始写这个脚本的,刚学着写,手很生:p
没想到提个问把作者给招来了,激动中……