29

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

With comm-result.txt containing the output of:

comm -3 file1 file2

But this doesn't seem to work.

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

How can i only get the first column from comm?

kenorb
  • 155,785
  • 88
  • 678
  • 743
Daddou
  • 379
  • 1
  • 4
  • 13

3 Answers3

35

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

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

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

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
Community
  • 1
  • 1
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
8

cut(1) is probably a better choice than awk for this problem.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
3

You can use comm with -2 and -3 (as already explained above), or use comm with grep like:

grep -o '^\S\+' <(comm file1 file2)

so the output won't contain any trailing spaces. This is useful for non-comm commands.

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • 1
    Works super good on ps output with just a little tweak! `ps -A | grep node | grep -o '^\s*\S\+'` – B T Apr 14 '16 at 01:26