I'm trying to write a bash script to read a file as input and append the values/data in that file into variables and the values of the variables should be updated each iteration based on the input line from the file.
Eg: Input file looks like below:
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
While reading this through my shell script, I'll define 5 variables let's a,b,c,d,e.
First iteration:
The variable values should be assigned with first line of input file.
a=100
b=Thomas
c=Manager
d=Sales
e=$5000
Second iteration:
The variable values should be assigned with second line of input file.
a=200
b=Jason
c=Developer
d=Technology
e=$5500
And so on...
Please anyone give some input on how to write a script to read values in this format.
You can use the
read
builtin command with option-a
with a loop to read each line of the file as an array. Then you can assign the value of the array elements to the variables you need (or you can use the array elements directly in your script):Another way is to assign the values of each column of certain line directly to the variables. The following example uses bash function that will feed the variables with new values when it is called. The function has one input parameter, that determines which line from the file to be parsed.
Notes:
The default value of
$IFS
is spaces and tabs, that is applicable in this case. For more details, please read this encyclopedic answer.The
-r
option used in the both examples, passed toread
command prevents backslash escapes from being interpreted.According to the usage of
sed
, within the second example, read this answer.For more examples, please see the previews version of the answer.