read input from a file and append that values to variables using shell
up vote
2
down vote
favorite
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.
command-line bash scripts
add a comment |
up vote
2
down vote
favorite
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.
command-line bash scripts
yeah @pa4080, I gone through it. Basically the first one suits pretty much to my scenario to make further modification with the script. As you mentioned in first answer I was missing -a with read command. Great! Thanks for the help
– harsha
Dec 5 at 10:10
1
Upvoted but forgot to tick
– harsha
Dec 5 at 10:14
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
command-line bash scripts
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.
command-line bash scripts
command-line bash scripts
edited Dec 5 at 7:47
pa4080
13.2k52561
13.2k52561
asked Dec 2 at 8:24
harsha
769
769
yeah @pa4080, I gone through it. Basically the first one suits pretty much to my scenario to make further modification with the script. As you mentioned in first answer I was missing -a with read command. Great! Thanks for the help
– harsha
Dec 5 at 10:10
1
Upvoted but forgot to tick
– harsha
Dec 5 at 10:14
add a comment |
yeah @pa4080, I gone through it. Basically the first one suits pretty much to my scenario to make further modification with the script. As you mentioned in first answer I was missing -a with read command. Great! Thanks for the help
– harsha
Dec 5 at 10:10
1
Upvoted but forgot to tick
– harsha
Dec 5 at 10:14
yeah @pa4080, I gone through it. Basically the first one suits pretty much to my scenario to make further modification with the script. As you mentioned in first answer I was missing -a with read command. Great! Thanks for the help
– harsha
Dec 5 at 10:10
yeah @pa4080, I gone through it. Basically the first one suits pretty much to my scenario to make further modification with the script. As you mentioned in first answer I was missing -a with read command. Great! Thanks for the help
– harsha
Dec 5 at 10:10
1
1
Upvoted but forgot to tick
– harsha
Dec 5 at 10:14
Upvoted but forgot to tick
– harsha
Dec 5 at 10:14
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
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):
#!/bin/bash
IN_FILE='./in-file.txt'
while read -ra LINE
do
a="${LINE[0]}"; b="${LINE[1]}"; c="${LINE[2]}"
d="${LINE[3]}"; e="${LINE[4]}"
echo -e "$an$bn$cn$dn$en"
done < "$IN_FILE"
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.
#!/bin/bash
IN_FILE='./in-file.txt'
args_feed() {
read -r a b c d e <<< "$(sed "$1 q;d" "$IN_FILE")"
}
# -----------------
# Examples of usage
# -----------------
# Parse the third line
args_feed 3
echo -e "$an$bn$cn$dn$en"
# Sequential call - First iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Sequential call - Second iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Parse the entire file
NR_LINES="$(cat "$IN_FILE" | wc -l)"
for LINE_NR in $(seq 1 $NR_LINES)
do
args_feed "$LINE_NR"
echo -e "$an$bn$cn$dn$en"
done
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.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1097849%2fread-input-from-a-file-and-append-that-values-to-variables-using-shell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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):
#!/bin/bash
IN_FILE='./in-file.txt'
while read -ra LINE
do
a="${LINE[0]}"; b="${LINE[1]}"; c="${LINE[2]}"
d="${LINE[3]}"; e="${LINE[4]}"
echo -e "$an$bn$cn$dn$en"
done < "$IN_FILE"
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.
#!/bin/bash
IN_FILE='./in-file.txt'
args_feed() {
read -r a b c d e <<< "$(sed "$1 q;d" "$IN_FILE")"
}
# -----------------
# Examples of usage
# -----------------
# Parse the third line
args_feed 3
echo -e "$an$bn$cn$dn$en"
# Sequential call - First iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Sequential call - Second iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Parse the entire file
NR_LINES="$(cat "$IN_FILE" | wc -l)"
for LINE_NR in $(seq 1 $NR_LINES)
do
args_feed "$LINE_NR"
echo -e "$an$bn$cn$dn$en"
done
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.
add a comment |
up vote
3
down vote
accepted
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):
#!/bin/bash
IN_FILE='./in-file.txt'
while read -ra LINE
do
a="${LINE[0]}"; b="${LINE[1]}"; c="${LINE[2]}"
d="${LINE[3]}"; e="${LINE[4]}"
echo -e "$an$bn$cn$dn$en"
done < "$IN_FILE"
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.
#!/bin/bash
IN_FILE='./in-file.txt'
args_feed() {
read -r a b c d e <<< "$(sed "$1 q;d" "$IN_FILE")"
}
# -----------------
# Examples of usage
# -----------------
# Parse the third line
args_feed 3
echo -e "$an$bn$cn$dn$en"
# Sequential call - First iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Sequential call - Second iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Parse the entire file
NR_LINES="$(cat "$IN_FILE" | wc -l)"
for LINE_NR in $(seq 1 $NR_LINES)
do
args_feed "$LINE_NR"
echo -e "$an$bn$cn$dn$en"
done
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.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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):
#!/bin/bash
IN_FILE='./in-file.txt'
while read -ra LINE
do
a="${LINE[0]}"; b="${LINE[1]}"; c="${LINE[2]}"
d="${LINE[3]}"; e="${LINE[4]}"
echo -e "$an$bn$cn$dn$en"
done < "$IN_FILE"
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.
#!/bin/bash
IN_FILE='./in-file.txt'
args_feed() {
read -r a b c d e <<< "$(sed "$1 q;d" "$IN_FILE")"
}
# -----------------
# Examples of usage
# -----------------
# Parse the third line
args_feed 3
echo -e "$an$bn$cn$dn$en"
# Sequential call - First iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Sequential call - Second iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Parse the entire file
NR_LINES="$(cat "$IN_FILE" | wc -l)"
for LINE_NR in $(seq 1 $NR_LINES)
do
args_feed "$LINE_NR"
echo -e "$an$bn$cn$dn$en"
done
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.
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):
#!/bin/bash
IN_FILE='./in-file.txt'
while read -ra LINE
do
a="${LINE[0]}"; b="${LINE[1]}"; c="${LINE[2]}"
d="${LINE[3]}"; e="${LINE[4]}"
echo -e "$an$bn$cn$dn$en"
done < "$IN_FILE"
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.
#!/bin/bash
IN_FILE='./in-file.txt'
args_feed() {
read -r a b c d e <<< "$(sed "$1 q;d" "$IN_FILE")"
}
# -----------------
# Examples of usage
# -----------------
# Parse the third line
args_feed 3
echo -e "$an$bn$cn$dn$en"
# Sequential call - First iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Sequential call - Second iteration
args_feed $((++LINE_NR))
echo -e "$an$bn$cn$dn$en"
# Parse the entire file
NR_LINES="$(cat "$IN_FILE" | wc -l)"
for LINE_NR in $(seq 1 $NR_LINES)
do
args_feed "$LINE_NR"
echo -e "$an$bn$cn$dn$en"
done
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.
edited Dec 4 at 19:36
answered Dec 2 at 11:12
pa4080
13.2k52561
13.2k52561
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1097849%2fread-input-from-a-file-and-append-that-values-to-variables-using-shell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
yeah @pa4080, I gone through it. Basically the first one suits pretty much to my scenario to make further modification with the script. As you mentioned in first answer I was missing -a with read command. Great! Thanks for the help
– harsha
Dec 5 at 10:10
1
Upvoted but forgot to tick
– harsha
Dec 5 at 10:14