#!/bin/bash # # Check that the data file exists # data="bash13_ex4.txt" [ -e "$data" ] || { echo "File $data not found"; exit 1; } # # Email addresses can be: # 1. local-part@domain # 2. Name # part1='([a-zA-Z0-9_][a-zA-Z0-9_.]+@[a-zA-Z0-9.-]+)' part2='([^<]+)<([a-zA-Z0-9_][a-zA-Z0-9_.]+@[a-zA-Z0-9.-]+)>' re="^($part1|$part2)$" # # Read and check each line from the file # while read -r line; do # # Does it match the regular expression? # if [[ $line =~ $re ]]; then #declare -p BASH_REMATCH # # Decide which format it is depending on whether element 2 of # BASH_REMATCH is zero length # if [[ -z ${BASH_REMATCH[2]} ]]; then # Type 2 name="${BASH_REMATCH[3]}" email="${BASH_REMATCH[4]}" else # Type 1 name= email="${BASH_REMATCH[2]}" fi echo "Name: $name" echo "Email: $email" else echo "Not recognised: $line" fi echo done < "$data"