Using awk
, first read the usernames of users whose account are locked from the second file, then extract the email addresses of these from the first file (then hope that they don't need to log in to read their emails):
awk -F ';' 'NR == FNR && $NF == 0 { names[$1] }
NR != FNR && $1 in names { print $NF }' B.csv A.csv
This assumes that each username has an equal amount of whitespace around them in both files. If that is not so, you may use -F ' *; *'
to include any space characters in the delimiter that awk
is using. It also assumes that there are no embedded ;
characters in the data.
NR
is the record (line) number of the current record overall, and FNR
is the same number but within the current file. If NR == FNR
, then we are reading from the first file given on the command line (B.csv
). NF
is the number of fields (columns) in the current record and $NF
is the data in the last field (and $1
is the data in the first field).
The code above uses an associative array/hash, names
, keyed on the usernames of locked-out users read from the first file (B.csv
). The $1 in names
will be true if $1
is a key in that array.
Putting this into a loop:
awk -F ';' 'NR == FNR && $NF == 0 { names[$1] }
NR != FNR && $1 in names { print $NF }' B.csv A.csv |
while read addr; do
printf 'Would send an email to "%s"\n' "$addr"
#mail -s 'Account locked' "$addr" <template-email.txt
done
Or something along those lines. Reading the email addresses in this way in the loop would delete any whitespace around them. The loop above does not send emails but prints the addresses that needs sending to. Remove the #
before mail
(and write some form email in template-email.txt
) to actually send an email (but you may want to do it differently).
Using csvkit
:
csvjoin -d ';' -c 1 A.csv B.csv |
csvgrep -c 5 -m False |
csvcut -S -c 3 | sed 1d
CSVkit provides CSV parsing tools for working with CSV files. This would be needed if your CSV data is not "simple", i.e. if it uses CSV rules for quoting embedded ;
characters etc. The pipeline above will
- Join the two files on the usernames (whitespaces are significant).
- Extract the data for the users that are locked out (the
0
will have been changed to False
at this point in the pipeline).
- Extract the email addresses.
- Remove the CSV header (using the last
sed
command).