Copache
Copache

Reputation: 23

UNIX (Bash): Splitting Strings into variables to be used in text

I am working on a project in which I need to have portions of a data file read into variables (last name, first name, room, etc) to be displayed in a paragraph, which is then looped until every name is covered.

The datafile is:

James,Robert,M,E162K,5101 Evergreen, Dearborn,Mi,48128
Fulton,Brent,M,E162I,5101 Evergreen, Dearborn,Mi,48128
Conner,Marrci,F,P262J,5101 Evergreen, Dearborn,Mi,48128
Conti,Anthony,M,P252F,5101 Evergreen, Dearborn,Mi,48128

And it needs to be fed into the following text: Where the variables listed are replaced with the corresponding names:

echo "Dear Mr/Mrs. $lastName" >>project2.output
echo "Welcome to Widgets, Inc. $firstName. This letter is to inform you of your assigned office space at
Widgets, Inc. is in the main $building  building. Your office is $room located at 5101 Evergreen,
Dearborn Mi. 48121.

I am not sure how to do this so any help would be much appreciated!

Upvotes: 0

Views: 128

Answers (1)

NinjaGaiden
NinjaGaiden

Reputation: 3146

What about this?

#!/usr/bin/env bash

while IFS="," read -r firstName lastName Sex building address city state zip; do
    echo -ne "Dear Mr/Mrs. $lastName, \n"
    echo "Welcome to Widgets, Inc. $firstName. This letter is to inform you of your assigned office space at Widgets, Inc. is in the main $building  building. Your office is $room located at $address,$city $state $zip"
done < datafile

Upvotes: 5

Related Questions