使用剪切和粘贴交换列(using cut and paste to swap column)

编程入门 行业动态 更新时间:2024-10-27 12:36:23
使用剪切和粘贴交换列(using cut and paste to swap column)

我有一个像下面的文件,我想交换第二列和第三列。

1425 Juan 14.25 4321 George 21.11 6781 Anna 16.77 1451 Ben 21.77 2277 Tuan 18.77

这是我用过的方式,它可以工作,但它是用两行书写的。

cut -f1,3 test1.txt > cat cut -f2 test1.txt | paste cat -> result

有什么方法可以将它写入一行吗?

评论:不能使用AWK来做到这一点,只能剪切和粘贴。

I got a file like below, I want to swap the second column and the third column.

1425 Juan 14.25 4321 George 21.11 6781 Anna 16.77 1451 Ben 21.77 2277 Tuan 18.77

Here's the way I used, it works but it's written in two lines.

cut -f1,3 test1.txt > cat cut -f2 test1.txt | paste cat -> result

Is there any way that I can write it in one line?

Comments: Cannot use AWK to do that, only cut and paste.

最满意答案

你可以连接命令; 或&& :

cut -f1,3 test1.txt > cat && cut -f2 test1.txt | paste cat -> result cut -f1,3 test1.txt > cat ; cut -f2 test1.txt | paste cat -> result

或者,使用支持进程替换的shell:

paste <(cut -f1 test1.txt) <(cut -f3 test1.txt) <(cut -f2 test1.txt)

You can just connect the commands with ; or &&:

cut -f1,3 test1.txt > cat && cut -f2 test1.txt | paste cat -> result cut -f1,3 test1.txt > cat ; cut -f2 test1.txt | paste cat -> result

Or, use a shell that supports process substitution:

paste <(cut -f1 test1.txt) <(cut -f3 test1.txt) <(cut -f2 test1.txt)

更多推荐

本文发布于:2023-07-04 17:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:paste   cut   column   swap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!