Skip to content

Instantly share code, notes, and snippets.

@DanielAdeniji

DanielAdeniji/readfile.sh

Last active Oct 19, 2020
Embed
What would you like to do?
Bash - Readfile
#!/bin/bash
#set name of datafile
filedata="datafile/blackbook.txt"
#set the Internal File Separator
#in our case set to semi colon (;)
IFS=';'
#set line number
lineNumber=0
#does file exist
#https://linuxize.com/post/bash-check-if-file-exists/
if [ ! -f "$filedata" ]; then
printf "file (%s) does not exist! \n" $filedata
exit
fi
printf "%3s %-15s %-15s %-15s \n" "###" "First Name" "Last Name" "Email"
printf "%3s %-15s %-15s %-15s \n" "---" "----------" "---------" "-----"
#read file
#read fullname, emailAddress
while read -r fullname emailAddress
do
#increment line number
lineNumber=$((lineNumber+1))
# using the cut command the first column
firstname=$(echo "$fullname" | awk -F ' ' '{print $1}' )
# using the awk command get the second column
lastname=$(echo "$fullname" | awk -F ' ' '{print $2}' )
#display read line
printf "%3d) %-15s %-15s %-15s \n" "$lineNumber" "$firstname" "$lastname" "$emailAddress"
#we are done at EOF
done < "$filedata"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.