To read fields within each line of the file, additional variables may be used with the read:
If an input file has 3 columns separated by white-space (space or tab characters only).
while read -r first_name last_name phone; do # Only print the last name (second column). printf '%s\n' "$last_name" done < "$file"
If the field delimiters are not whitespace, set the IFS (internal field separator):
# Extract the username and its shell from /etc/passwd: while IFS=: read -r user pass uid gid gecos home shell; do printf '%s: %s\n' "$user" "$shell" done < /etc/passwd
NOTE: IFS is set to a colon, :, as every field in the passwd file is separated by a colon.
NOTE: For tab-delimited files, use IFS=$'\t'.
WARNING: Multiple tab characters in the input will be considered as one delimiter (and the IFS=$'\t\t' workaround does not work in Bash).
You do not necessarily need to know how many fields each line of input contains.
For example:
read -r first last junk <<< 'Bob Smith 123 Main Street Saint Helier Jersey'
NOTE:
read -r _ _ first middle last _ <<< "$record"
NOTE: The throwaway variable _ can be used as a “junk variable” to ignore fields.
WARNING: This usage of _ is only guaranteed to work in Bash.