简体   繁体   中英

How to differentiate between two files in shell script

I have a file a.txt which has

a,20
b,30
c,40

Another b.txt has

a,40
b,60
d,3

I need to iterate the first column and check if a variable is not present in the second file (here c is not present) and send out a mail with that variable name.

To extract just the first column, you can use cut :

cut -d, -f1 file.txt
  • -d specifies the delimiter
  • -f specifies columns to extract

To check for lines common to a pair of files, use comm :

comm file1 file2

comm outputs three columns: the 1st one lists things unique to file1, the 2nd one things unique to file2, the 3rd things common to both. You can tell comm to omit a column by specifying its number as an argument. comm needs the input files to be sorted, though. I usually set $LC_ALL to C when sorting to avoid any influence of the locale.

So, the answer to your question is

comm -23 <(cut -f1 -d, a.txt | LC_ALL=C sort) <(cut -f1 -d, b.txt | LC_ALL=C sort)

可能的awk解决方案:

awk -F, 'FNR==NR{a[$1];next}!($1 in a){print $1}' b.txt a.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM