grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt
would only produce the lines from the original data that contains the string
Chrome anywhere on the line. This is because the result of the first
grep in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).
grep will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first
sort -k2r LoginExcInternal.txt >test.txt
You want the lines with
Chrome in their 2nd column to come after the lines containing
OpenFin in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.
-k2r flag to
sort tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the
r). If two lines have the same value in the second column, then the whole line will be used as the sorting key.
Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.
If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:
awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt
This applies the same
awk code twice on the original data, with different values in the variable
value. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the
awk script does the
OpenFin values, and the second adds the
Chrome lines to this.
This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be
OpenFin. Also, since the
awk code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.