How to Get the First Column of Comm Output

How to get the first column of comm output?

"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

How to print the first line of first column and second column as a single column with awk?

1st solution: I would go with following approach so that we can handle multiple ids too, written and tested with shown samples in GNU awk.

awk '{a[$1]=(a[$1]?a[$1] ORS:"")$2} END{for(i in a){print i ORS a[i]}}' Input_file

2nd solution: Considering if your Input_file's 1st field is sorted then try following.

awk 'prev!=$1{print $1 ORS $2;prev=$1;next} {print $2}' Input_file

3rd solution: In case your Input_file is not sorted with 1st column then sort it and run the awk code.

sort -k1 Input_file | awk 'prev!=$1{print $1 ORS $2;prev=$1;next} {print $2}'

getting first column of a matching line in bash

You may use awk like this:

name=$(kubectl get pods -n system | awk '/^my-pod.*Running/{print $1}')
[[ -n $name ]] && kubectl -n system logs "$name" --tail=5 -f

awk command will match pattern my-pod.*Running at the start of a line and if it is found then it will print first column. We store that in variable name.

If $name is not empty then we call kubectl -n system logs using that value.

How to get the first column of every line from a CSV file?

Try this:

 awk -F"," '{print $1}' data.txt

It will split each input line in the file data.txt into different fields based on , character (as specified with the -F) and print the first field (column) to stdout.

How to extract the first column from an output line?

sudo fdisk -l | tail -n 1 | awk '{print $1}'

will produce as

/dev/sdb1

Compare first column of one file with the first column of second and print associated column of each if there was a match

Could you please try following.

awk 'FNR==NR{a[$1]=$2;next} ($1 in a){print $2,a[$1]}' Input_file1  Input_file2

Output will be as follows.

foo 1589.0
hi 33.7

Problem in your attempt: You was going good only thing in FNR==NR condition your a[$1] is NOT having any value it only created its index in array a so that is why it was not able to print anything when 2nd Input_file is being read.

How to get the second column from command output?

Or use sed & regex.

<some_command> | sed 's/^.* \(".*"$\)/\1/'


Related Topics



Leave a reply



Submit