brucezepplin
brucezepplin

Reputation: 9752

select rows where values of two columns agree

if I have the following:

1 5 a
2 5 a
3 5 a
4 5 a
5 5 a
6 5 a
1 3 b
2 3 b
3 3 b
4 3 b
5 3 b
6 3 b

How do I only select rows where the two columns have the same value i.e.

5 5 a
3 3 b

in bash / awk / sed.

I know how to select rows with certain values using awk, but only when I specifiy the value.

Upvotes: 2

Views: 700

Answers (4)

glenn jackman
glenn jackman

Reputation: 246744

For completeness:

bash

while read first second rest; do
    [[ $first -eq $second ]] && echo "$first $second $rest"
done < fi

or if content is not just integers:

while read first second rest; do
    [[ $first == $second ]] && echo "$first $second $rest"
done < file

sed

sed -En '/^([^ ]+) \1 /p' file

Upvotes: 2

potong
potong

Reputation: 58361

This might work for you (GNU grep):

grep '^\(\S\+\)\s\+\1\s\+' file

Upvotes: 0

mpapec
mpapec

Reputation: 50637

perl -ane 'print if $F[0] == $F[1]' file

Upvotes: 4

fedorqui
fedorqui

Reputation: 289495

Just say:

$ awk '$1==$2' file
5 5 a
3 3 b

As you see, when the condition $1 == $2 is accomplished, awk automatically prints the line.

Upvotes: 4

Related Questions