交换两列 - AWK,sed的,蟒蛇,perl的 [英] Swap two columns - awk, sed, python, perl

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

问题描述

我有一个大文件中的数据(宽280列700万线长!),我需要交换前两列。我想我能以某种awk的for循环的做到这一点,打印$ 2,$ 1,则范围在文件的结尾 - 但我不知道该怎么办了一系列的一部分,我不能打印$ 2 ,$ 1,$ 3 ... $ 280!大多数列交换答案,我在这里看到的是特定的列的可管理的一些小文件,所以我需要的东西,不依赖于指定每列数。

该文件是制表符分隔:

  AFFY-ID CHR 0 POS NA06984 NA06985 NA06986 NA06989


解决方案

您可以通过交换前两个字段的值做到这一点:

 的awk'{T = $ 1; $ 1 = $ 2; $ 2 =吨;打印; }'INPUT_FILE

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.

The file is tab delimited:

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的,蟒蛇,perl的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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