使用 cut 重新排列列 [英] Rearrange columns using cut

查看:30
本文介绍了使用 cut 重新排列列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的文件

<前>列 1 列 2字符串 1 1str2 2str3 3

我希望重新排列列.我试过下面的命令

<块引用>

cut -f2,1 file.txt

该命令不会对列重新排序.知道为什么它不起作用吗?

解决方案

对于 cut(1) 手册页:

<块引用>

使用 -b、-c 或 -f 中的一种,并且仅使用一种.每个 LIST 由一范围,或多个以逗号分隔的范围.选择的输入被写入与读取顺序相同,并且只写入一次.

它首先到达字段 1,因此打印出来,然后是字段 2.

改用awk:

awk '{ print $2 " " $1}' file.txt

I am having a file in the following format

Column1    Column2
str1       1
str2       2
str3       3

I want the columns to be rearranged. I tried below command

cut -f2,1 file.txt

The command doesn't reorder the columns. Any idea why its not working?

解决方案

For the cut(1) man page:

Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Selected input is written in the same order that it is read, and is written exactly once.

It reaches field 1 first, so that is printed, followed by field 2.

Use awk instead:

awk '{ print $2 " " $1}' file.txt

这篇关于使用 cut 重新排列列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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