gpt4 book ai didi

linux - 使用 awk 反转字段

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:56 25 4
gpt4 key购买 nike

我需要反转包含以下内容的文本文件的字段:

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 ""
}

以上假设您不关心字段之间的空白。

关于linux - 使用 awk 反转字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com