交换两列 - awk、sed、python、perl [英] Swap two columns - awk, sed, python, perl

查看:29
本文介绍了交换两列 - awk、sed、python、perl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大文件(280 列宽,700 万行!),我需要交换前两列.我想我可以用某种 awk for 循环来做到这一点,打印 $2、$1,然后是到文件末尾的范围 - 但我不知道如何做范围部分,我不能打印 $2,1美元,3美元……280美元!我在这里看到的大多数列交换答案都特定于具有可管理的列数的小文件,因此我需要一些不依赖于指定每个列号的东西.

I've got data in a large file (280 columns wide, 7 million lines long!) and I need to swap the first two columns. I think I could do this with some kind of awk for loop, to print $2, $1, then a range to the end of the file - but I don't know how to do the range part, and I can't print $2, $1, $3...$280! Most of the column swap answers I've seen here are specific to small files with a manageable number of columns, so I need something that doesn't depend on specifying every column number.

文件以制表符分隔:

Affy-id chr 0 pos NA06984 NA06985 NA06986 NA06989

推荐答案

您可以通过交换前两个字段的值来实现:

You can do this by swapping values of the first two fields:

awk ' { t = $1; $1 = $2; $2 = t; print; } ' input_file

这篇关于交换两列 - awk、sed、python、perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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