如何获得第一行的comm输出? [英] How to get the first column of comm output?

查看:90
本文介绍了如何获得第一行的comm输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试使用awk获取comm输出的第一列. 我读到Tab用作逗号分隔符,所以我这样做了:

So I'm trying to get the first column of comm output using awk. I read that Tab was used as a separator for comm so I did:

awk -F"\t" '{print $1}' comm-result.txt

comm-result.txt包含以下输出:

With comm-result.txt containing the output of:

comm -3 file1 file2

但这似乎不起作用.

此建议还使用空格字符作为分隔符,当我的文件包含多个空格时,我得到奇怪的结果.

This commend takes also the space character as a separator and I get weird results when my files contains multiple spaces.

我怎么只能从comm中获得第一列?

How can i only get the first column from comm?

推荐答案

因此,我正在尝试获取comm输出的第一列"

"So I'm trying to get the first column of comm output"

"comm file1 file2"输出的第一列包含file1唯一的行.您可以通过简单地用-2(file2特有的抑制行)和-3(两个文件中均出现的抑制行)调用comm来跳过后处理.

The first column of the "comm file1 file2" output contains lines unique to the file1. You can skip the post-processing by simply calling comm with -2 (suppress lines unique to file2) and -3 (suppress lines that appear in both files).

comm -2 -3 file1 file2   # will show only lines unique to file1


但是,如果您别无选择,只能处理comm的运行前输出,则如提到卡尔 ,则可以选择cut:


However, if you have no choice but to process a pre-run output of comm then as Carl mentioned, cut would be an option:

cut -f1 comm-results.txt

但是,对于第1列为空的情况,这将导致空行.为了解决这个问题,也许awk可能更合适:

However, this result in empty lines for cases where column 1 is empty. To deal with this, perhaps awk may be more suitable:

awk -F"\t" '{if ($1) print $1}' comm-results.txt
     ----    ----------------
      |                     |
   Use tab as delimiter     |
                            +-- only print if not empty

这篇关于如何获得第一行的comm输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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