Linux Shell命令可反转可变长度文本记录的字段顺序 [英] Linux shell command to reverse the field order of varying length text records

查看:105
本文介绍了Linux Shell命令可反转可变长度文本记录的字段顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过命令行来反转任意长度文本记录的字段顺序. 使用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 -lane 'print join " ", reverse @F' file
d c b a
f e
i h g

但是,正如您在评论中所解释的那样,如果您想获得最新的3列,则可以使用:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆