# How to compare the 1st column of two files and if the strings match then print "true" [duplicate]

I have two files, say file1 and file2.

``````file1:

91 | 2018-10-11 16:07:14 | bkwdv | 188.0000 |
77 | 2018-10-12 15:05:15 | wvwef | 94.0000  |

file2:

77 | 2018-10-12 15:05:15 | wvwef | 94.0000
91 | 2018-10-11 16:07:14 | bkwdv | 188.0000
``````

I would like to compare the first column of file1 with the first column of file2.

In case the strings match (irrespective of the order, as in the given input data), it should print "success", else "exit". So, as per the given input data, it should print "success".

Please note that the number of rows in the first column could vary in either of the files, so in this case, it should exit.

• Please, give us more detail: how many rows is compared, does the number of rows equal, what if in file1 is more rows with the same 1st. column . . .? And what exactly means "des not match"? Oct 12, 2018 at 12:11
• I've edited my question ... there could be n number of rows in either of the files, the only criteria for printing "success" is that 1st column of file1 and file2 should be having same number of rows and same strings irrespective of the order of the strings. Oct 12, 2018 at 12:19
• @Goro: my previous Question was bit different from the present one, this time i was bit more concerned about the ordering of the strings of col1 in file1 and file2. Thanks Oct 13, 2018 at 9:32

Extract column one from each file, sort them numerically, and compare them:

``````awk '{ print \$1 }' <file1 | sort -n >file1.col1
awk '{ print \$1 }' <file2 | sort -n >file2.col1

if cmp -s file1.col1 file2.col1; then
echo 'success'
else
echo 'exit'
fi

rm file1.col1 file2.col1
``````

Or, using `bash`-specific syntax:

``````if cmp -s <( awk '{ print \$1 }' <file1 | sort -n ) \
<( awk '{ print \$1 }' <file2 | sort -n )
then
echo 'success'
else
echo 'exit'
fi
``````

The `cmp -s` command will not output anything but will exit successfully if its two file operands have identical contents. We use this in an `if` statement to figure out whether the sorted 1st columns of the two files are identical or not.

`awk '{ print \$1 }'` in the code above could also be replaced by `cut -d ' ' -f 1` if the whitespace character after the number in the first column is a space character.

``````#!/bin/bash
var=\$(cut -d"|" -f 1 file  | sort -k1.1n)
var1=\$(cut -d"|" -f 1 file | sort -k1.1n)

if [ "\$var" == "\$var1" ]
then
echo "success"
else
exit
fi
``````

Explanation💡:

The delimiter `-d` in the command `cut` is set to a `|` with `-d |`, so `cut` can see the borders between the columns which are `|`. Then `cut` can pull out the fields of interest in column #1 with the `-f` flag.

In this example, we want the command `sort` to compare a limited subset of the data (i.e. data in column #1). For so, we specified which fields to compare using a key called the `-k` option. `k1.1` will sort the first field, and ignore the rest columns. In other words, sort on column #1 to column #1 (i.e. sort only column #1).

According to sort man

``````-k, --key=POS1[,POS2]   Where POS1 is the starting field position, and POS2 is the ending field position
``````
• You can't throw away data, which you may do with `sort -u`. If you do, a file with the numbers 1, 1, 2 would compare equal to a file with 1, 2. Oct 13, 2018 at 9:05
• @Kusalananda THANK YOU!, I revised by replacing `-u` with `-k1.1n`.
– user88036
Oct 13, 2018 at 9:15
• @Goro: can you please elaborate more on this part: `-k1.1n` used in the `sort` command. Oct 13, 2018 at 9:50

You could check for unpairable lines in the sorted files using `join`, and test whether the result is the empty string:

``````[[ -z "\$(join -t '|' -v1 -v2 <(sort file1) <(sort file2))" ]] && echo "success" || echo "exit"
``````

Well in such conditions

1. cat both files
2. sort -n uniqe
3. count numer of result lines

If you got the sam number as the number of lines in both files, you got succes.

``````N1=\$(cat file1 |wc -l) ;N2=\$(cat file2 |wc -l); N3=\$(cat file1 file2 |sort -nu |wc -l); echo \$N1 \$N2 \$N3
``````

If you need to get result: success / exit , you must compare the N1, N2, N3 yourself. Of course, it is recomended to compare N1 and N2 before you start to gether the result N3, and if equals, and you continue, then you compare only N1 with N3 (because you start count the N3 and thats why N1==N2).

• None of the `cat`s are necessary. Oct 13, 2018 at 8:26
• Interesting approach. With `1`, `1`, `2` in both files it seems to fail (I get `3 3 2` as the result). Oct 13, 2018 at 10:30
• @roaima Even asked for detail, User123 did not let us know, if we can expect two equal prefixes (=first column) in one file (more lines with the same col1 ~ your case). The best soulution is of course that of Kusalananda with `cmp`, or similar with `diff`. Writing good programs needs analysis first. Heavy analysis. You cannot analyse without facts. I got a crystal ball, but without manual. Oct 15, 2018 at 13:45