Kev的解决方案看起来是将每个单词中的文本反转。你的示例输出没有显示出来,但他的关键点是使用一个函数。
你已经有了所需的代码,只需要稍微重新排列一下。
cat file1
aa bb cc
foo do as
cat commandFile
function reverse( line ) {
n=split(line, tmpLine)
for (j=n; j>0; j--) {
printf("%s ",tmpLine[j] )
}
}
{ a[NR]=$0 }
END{ for(i=NR; i>0; i--) printf( "%s\n", reverse(a[i]) ) }
运行中
awk -f commandFile file1
输出
as do foo
cc bb aa
我进行了一些小修改,使用n=split(line, tmpLine) ... print tmpLine[j]
是在函数中解析输入行并打印输出的常见方法。 我不认为$1变量从传递的值(您的a [i]值)中具有范围,因此我将其更改为split..tmpLine [j]。 我还发现END部分的“i”变量在函数reverse
中保持了范围,因此我将其更改为j以消除歧义情况。
我不得不弄清楚一些事情,下面是我使用的调试版本。
如果您要访问gawk,则最好学习如何使用可用的调试器。 如果您在没有调试器的系统上使用awk / gawk / nawk,则这是了解代码中正在发生的情况的一种方法。 如果您将程序的输出重定向到文件或管道,并且如果您的系统支持“/ dev / stderr”表示法,则可以在那里打印调试行,例如:
#dbg print "#dbg0:line=" line > "/dev/stderr"
一些系统在访问标准错误输出时采用了不同的符号表示方法,如果你需要经常这样做,那么了解其它可用方式是值得的。
cat commandFile.debug
function reverse( line ) {
n=split(line, tmpLine)
#dbg print "#dbg0:line=" line
#dbg print "#dbg1:n=" n "\tj=" j "\ttmpLine[j]=" tmpLine[j]
for (j=n; j>0; j--) {
#dbg print "#dbg2:n=" n "\tj=" j "\ttempLine[j]=" tmpLine[j]
printf("%s ",tmpLine[j] )
}
}
# main loop
{ a[NR]=$0 }
# print reversed array
#dbg END{ print "AT END"; for(i=NR; i>0; i--) printf( "#dbg4:i=%d\t%s\n%s\n", i, a[i] , reverse(a[i])
) }
END{ for(i=NR; i>0; i--) printf( "%s\n", reverse(a[i]) ) }
我希望这有所帮助。