我想使用 awk
获取 comm 输出的第一列。我了解到 Tab 被用作 comm 的分隔符,因此我执行了以下命令:
awk -F"\t" '{print $1}' comm-result.txt
使用comm-result.txt存储以下输出结果:
comm -3 file1 file2
但是这似乎不起作用。
这个命令也以空格字符作为分隔符,当我的文件包含多个空格时,我会得到奇怪的结果。
我如何只从comm
中获取第一列?
我想使用 awk
获取 comm 输出的第一列。我了解到 Tab 被用作 comm 的分隔符,因此我执行了以下命令:
awk -F"\t" '{print $1}' comm-result.txt
使用comm-result.txt存储以下输出结果:
comm -3 file1 file2
但是这似乎不起作用。
这个命令也以空格字符作为分隔符,当我的文件包含多个空格时,我会得到奇怪的结果。
我如何只从comm
中获取第一列?
comm -2 -3 file1 file2 # will show only lines unique to file1
不过,如果你只能处理 comm
的预运行输出,那么像 Carl mentioned 提到的,可以考虑使用 cut
命令:
cut -f1 comm-results.txt
awk
:awk -F"\t" '{if ($1) print $1}' comm-results.txt
---- ----------------
| |
Use tab as delimiter |
+-- only print if not empty
cut(1)
对于这个问题可能比 awk
更好。
您可以使用comm
和-2
以及-3
(如上面已经解释过的),或者使用comm
与grep
结合使用,如下所示:
grep -o '^\S\+' <(comm file1 file2)
因此,输出内容不会包含任何尾随空格。这对于非 comm
命令非常有用。
ps -A | grep node | grep -o '^\s*\S\+'
- B T