linux - 如何获取通信输出的第一列?

标签 linux shell command-line comm

所以我尝试使用 awk 获取通信输出的第一列。 我读到 Tab 被用作 comm 的分隔符,所以我做了:

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

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

comm -3 file1 file2

但这似乎行不通。

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

我怎样才能只从 comm 获取第一列?

最佳答案

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

comm file1 file2”输出的第一列包含 file1 独有的行。您可以通过使用 -2(抑制 file2 独有的行)和 -3comm 来跳过后处理(隐藏出现在两个文件中的行)。

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

但是,如果您别无选择,只能处理 comm 的预运行输出,则作为 Carl mentioned , cut 是一个选项:

cut -f1 comm-results.txt

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

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

关于linux - 如何获取通信输出的第一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8299553/

相关文章:

shell - 在 shell 脚本中优化 grep(或使用 AWK)

linux - Grep/Sed : How to do a recursive find/replace of a string?

linux - 将重复的数字打印到另一个文件

command-line - PowerShell 2.0 - 运行命令行调用与从 ISE 调用的脚本

linux - 使用 glibc 定时器时出现段错误

linux - 系统重启后可以连接 screen 吗?

linux - 如何永久杀死docker?

linux - arm-linux 下缺少 libopencv_ts 缺失

linux - 在命令行中将参数传递给 ksh 脚本时转义反引号 - grave(`)

linux - 在 Linux 上的目录中获取最新文件