如何打印包含值列 [英] How to print columns containing value

查看:118
本文介绍了如何打印包含值列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个包含数据文件中的以下内容:

Let's say I have a data file containing the following:

 1     2     3     4     5
67    88    12    32    22
 9    99    34    59    86
17     0    78     0    77
11     0     0     0    43

我想有一个code到人数为0的每一列如果发现数字0,搜索时,code将打印出整列在一个单独的文件中。

I would like to have a code that searches through each column for the number 0. If the number 0 is found, the code will print out that entire column in a separate file.

有了这些数据,输出的文件看起来像这样:

With this data, the outputted file would look like so:

 2     3     4
88    12    32
99    34    59
 0    78     0
 0     0     0     

这将会是巨大的,如果code并没有要求我知道列和/或行的确切数目。

It'd be great if the code didn't require me knowing the exact number of columns and/or row.

推荐答案

这会做你想要什么。它并不需要知道对多行或列是如何present什么。

This will do what you want. It does not requiring knowing anything about how many rows or columns are present.

$ awk 'FNR==NR{for (i=1;i<=NF;i++)if ($i==o)a[i]=1;next} {tab="";for (i=1;i<=NF;i++)if (a[i]){printf "%s%s",tab,$i; tab="\t"};print ""}' file file
2       3       4
88      12      32
99      34      59
0       78      0
0       0       0

工作原理

由于文件名被指定了两次在命令行中, AWK 脚本将读取该文件两次,第一次去找零,第二次打印

How it works

Because the file name is specified twice on the command line, the awk script will read the file twice, the first time to look for zeros, the second time to print.


  • FNR == NR {为(i = 1; I&LT; = NF;我++),如果($ I = = O)A [i] = 1;接下来}

一个通过文件第一次运行时, A [I] 设置为一个任意列 I 的具有零在它

One the first run through the file, a[i] is set to one for any column i that has a zero in it.

这code仅适用于通过,因为条件 FNR == NR 第一次运行。 NR 是我们迄今阅读记录(行)的总数。 FNR 是我们从当前文件至今读的记录(行)的数量。因此,当 FNR == NR ,我们还在读书的第一个文件。在接下来在命令的结尾告诉 AWK 来跳过余下的命令,并重新开始下一行。

This code only applies to the first run through because of the condition FNR==NR. NR is the total number of records (lines) that we have read so far. FNR is the number of records (lines) that we have read so far from the current file. Thus, when FNR==NR, we are still reading the first file. The next at the end of the commands tells awk to skip the remaining commands and start over on the next line.

设置页=;对于(i = 1; I&LT; = NF;我++),如果(A []​​){p​​rintf的%s%S选项卡中,$ I ;标签=\\ t};打印,

当我们通过第二次文件中读取数据,我们打印出每列 I 为其 A [I] 是非零。我选择了制表符分隔的输出,但通过简单地调整<​​code>的printf 语句,任何格式都可以使用。

When we are reading through the file for the second time, we print out each column i for which a[i] is non-zero. I chose tab-separated output but, by simply adjusting the printf statement, any format could be used.

这篇关于如何打印包含值列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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