[英]Reversing a field with awk
我需要反转包含以下内容的文本文件的字段:
Computer science is fun
I also enjoy math
But I don't like science
我需要输出awk
:
line 1: Computer science is fun
line 1 reversed: fun is science Computer
line 2: I also enjoy math
line 2 reversed: math enjoy also I
....... 等等。 这就是我现在所拥有的:
BEGIN {print "start"}
{
for(i=NF; i>=1; i--)
{
printf "line %d: %s\n", NR , $0;
printf "line %d: reversed: %s", NR, $i;
}
}
你很亲密:
BEGIN {print "start"}
{
printf "line %d: %s\n", NR , $0
printf "line %d: reversed:", NR
for(i=NF; i>=1; i--)
{
printf " %s", $i
}
print ""
}
以上假设您不关心字段之间的空白区域。
尝试:
awk '{for (i=NF; i>0; i--) printf("%s ",$i);print ""}' input.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.