Linux Shell命令可反转可变长度文本记录的字段顺序 [英] Linux shell command to reverse the field order of varying length text records
问题描述
我想通过命令行来反转任意长度文本记录的字段顺序. 使用cut的重新排列列中提供的解决方案和
I would like a command line incantation to reverse the field order of arbritrary length text records. Solutions provided in Rearrange columns using cut and Elegant way to reverse column order don't solve this issue since they assume a fixed amount of fields, though maybe they would with minor changes.
类似于tac
命令的排序,具有反向cat
功能.我想使用ohce
命令(如果存在的话)来反转echo
功能.
Sort of like the tac
command that exhibits reverse cat
functionality. I'd like what the ohce
command would do (if it existed) to reverse echo
functinality.
例如:
a b c d
e f
g h i
应转换为
d c b a
f e
i h g
推荐答案
There's a command to do it, it's named rev
from util-linux :
$ rev file
d c b a
f e
i h g
或使用 perl 的问题:
$ perl -lane 'print join " ", reverse @F' file
d c b a
f e
i h g
但是,正如您在评论中所解释的那样,如果您想获得最新的3列,则可以使用 awk :
But like you explain in the comments, if you want the 3 latest columns, you can use awk :
awk '{print $(NF-2), $(NF-1), $NF}' file
这篇关于Linux Shell命令可反转可变长度文本记录的字段顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!