Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first column of comm output?

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?

like image 426
Daddou Avatar asked Nov 28 '11 17:11

Daddou


People also ask

How do I display the first column?

On the Home tab, in the Cells group, click Format. Do one of the following: Under Visibility, click Hide & Unhide, and then click Unhide Rows or Unhide Columns.

How do I get the first column in awk?

awk to print the first column. The first column of any file can be printed by using $1 variable in awk. But if the value of the first column contains multiple words then only the first word of the first column prints. By using a specific delimiter, the first column can be printed properly.

How do I get only the first column in Linux?

Here, \t is used as a field separator to print the first column of the file. The '-F' option is used to set the field separator. The following output will be produced after running the above commands.

How do I get the first column in bash?

You can use awk 'NR > 1 {print $1}' to get the first column (using any whitespace character as a delimiter while skipping the first line) or sed 1d | cut -d$'\t' -f1 .


3 Answers

"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 
like image 51
Shawn Chin Avatar answered Sep 20 '22 05:09

Shawn Chin


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

like image 37
Carl Norum Avatar answered Sep 22 '22 05:09

Carl Norum


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.

like image 42
kenorb Avatar answered Sep 20 '22 05:09

kenorb