Reputation: 9752
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
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
Reputation: 58361
This might work for you (GNU grep):
grep '^\(\S\+\)\s\+\1\s\+' file
Upvotes: 0
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