<p>The Perl solution is similar to the awk solution from <em>MaximilianPress</em>:</p>
<p>Print lines with identical values in columns 2 and 3:</p>
<pre><code>perl -lane 'print if <span class="math-container">$F[1] == $</span>F[2];' in.txt

# Prints:
cont1005 3642 3642
cont1037 3146 3146
cont1056 934 934
cont1059 1750 1750
cont1072 2577 2577
</code></pre>
<p>Print lines with different values in columns 2 and 3:</p>
<pre><code>perl -lane 'print if <span class="math-container">$F[1] != $</span>F[2];' in.txt

# Prints:
cont100 1128 1125
cont1006 103 19
cont104 895 890
cont1078 43 42
cont1081 727 2
</code></pre>
<p>The Perl one-liner uses these command line flags:<br />
<code>-e</code> : Tells Perl to look for code in-line, instead of in a file.<br />
<code>-n</code> : Loop over the input one line at a time, assigning it to <code>$_</code> by default.<br />
<code>-l</code> : Strip the input line separator (<code>&quot;\n&quot;</code> on *NIX by default) before executing the code in-line, and append it when printing.<br />
<code>-a</code> : Split <code>$_</code> into array <code>@F</code> on whitespace or on the regex specified in <code>-F</code> option. Arrays in Perl are 0-indexed.</p>
<p><strong>SEE ALSO:</strong><br />
<a href="https://perldoc.perl.org/perlrun.html#Command-Switches" rel="nofollow noreferrer"><code>perldoc perlrun</code>: how to execute the Perl interpreter: command line switches</a></p>

CC BY-SA 4.0
2020-10-03T15:41:57.663Z
2020-10-03T15:41:57.663Z
Timur Shtatland
<p>I think it is easy to do with awk, e.g. see <a href="https://stackoverflow.com/questions/42092693/awk-concise-way-to-check-if-2-fields-match-the-same-string">here</a>.</p>
<p>For your example, with your data in <code>file</code>:</p>
<pre><code>%  awk '<span class="math-container">$2 != $</span>3 {print $0}' file
# outputs
cont100 1128 1125
cont1006 103 19
cont104 895 890
cont1078 43 42
cont1081 727 2

%  awk '<span class="math-container">$2 == $</span>3 {print $0}' file
# outputs:
cont1005 3642 3642
cont1037 3146 3146
cont1056 934 934
cont1059 1750 1750
cont1072 2577 2577

</code></pre>
<p>Note that where you say &quot;similar&quot; I am interpreting that to mean &quot;exactly the same&quot;, because that is the case in your example.</p>

CC BY-SA 4.0
2020-10-02T20:37:35.450Z
It worked and I got the result that I wanted. Thank you very much.
MudithMMBc
CC BY-SA 4.0
2020-10-02T19:49:48.030Z
2020-10-02T19:49:48.030Z
Maximilian Press
<p>I have following example data from a large file.</p>
<pre><code>cont100 1128 1125
cont1005 3642 3642
cont1006 103 19
cont1037 3146 3146
cont104 895 890
cont1056 934 934
cont1059 1750 1750
cont1072 2577 2577
cont1078 43 42
cont1081 727 2
</code></pre>
<p>I need to get only the rows with similar values in column 2 and column 3. The output should be like this.</p>
<pre><code>cont1005 3642 3642
cont1037 3146 3146
cont1056 934 934
cont1059 1750 1750
cont1072 2577 2577
</code></pre>
<p>Also I need to get the inverse of that. That means I need to get the rows with values that are not similar in the columns. The output should be like this.</p>
<pre><code>cont100 1128 1125
cont1006 103 19
cont104 895 890
cont1078 43 42
cont1081 727 2
</code></pre>

CC BY-SA 4.0
2020-10-03T15:35:02.420Z
Do you mean **identical** values in columns 2 and 3?
Timur Shtatland
<bash>
How to get rows with similar values in two different columns using command line?
# How to get rows with similar values in two different columns using command line?

(Asked by: username_0 on 2020-10-02)

I have following example data from a large file.
cont100 1128 1125
cont1005 3642 3642
cont1006 103 19
cont1037 3146 3146
cont104 895 890
cont1056 934 934
cont1059 1750 1750
cont1072 2577 2577
cont1078 43 42
cont1081 727 2

I need to get only the rows with similar values in column 2 and column 3. The output should be like this.
cont1005 3642 3642
cont1037 3146 3146
cont1056 934 934
cont1059 1750 1750
cont1072 2577 2577

Also I need to get the inverse of that. That means I need to get the rows with values that are not similar in the columns. The output should be like this.
cont100 1128 1125
cont1006 103 19
cont104 895 890
cont1078 43 42
cont1081 727 2

**Tags:** bash
> **Comments:**
> **username_1**: Do you mean **identical** values in columns 2 and 3?  

---

## 2 answers

### Answer by username_2 on 2020-10-02. Score: 8 | Accepted answer

I think it is easy to do with awk, e.g. see here.
For your example, with your data in file:
%  awk '$2 != $3 {print $0}' file
# outputs
cont100 1128 1125
cont1006 103 19
cont104 895 890
cont1078 43 42
cont1081 727 2

%  awk '$2 == $3 {print $0}' file
# outputs:
cont1005 3642 3642
cont1037 3146 3146
cont1056 934 934
cont1059 1750 1750
cont1072 2577 2577


Note that where you say "similar" I am interpreting that to mean "exactly the same", because that is the case in your example.

> **Comments:**
> **username_0**: It worked and I got the result that I wanted. Thank you very much.  

---

### Answer by username_1 on 2020-10-03. Score: 2

The Perl solution is similar to the awk solution from MaximilianPress:
Print lines with identical values in columns 2 and 3:
perl -lane 'print if $F[1] == $F[2];' in.txt

# Prints:
cont1005 3642 3642
cont1037 3146 3146
cont1056 934 934
cont1059 1750 1750
cont1072 2577 2577

Print lines with different values in columns 2 and 3:
perl -lane 'print if $F[1] != $F[2];' in.txt

# Prints:
cont100 1128 1125
cont1006 103 19
cont104 895 890
cont1078 43 42
cont1081 727 2

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-n : Loop over the input one line at a time, assigning it to $_ by default.
-l : Strip the input line separator ("\n" on *NIX by default) before executing the code in-line, and append it when printing.
-a : Split $_ into array @F on whitespace or on the regex specified in -F option. Arrays in Perl are 0-indexed.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches


---

