How can I read each line from a file into one variable for the last field, and another variable for the other fields?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
How can I read a line from a file into two variables: one variable for the last field, and the the other variable for the other fields?
For example, I have a file:
hello world! 10s
It is a good day. 4m
...
I would like to read each line into two variables: one variable contains the time interval at the end of the line, and the other variable contains the fields before it. I was wondering how to do it; this is as far as I have gotten:
while read line
do
... # split $line into $message and $interval
myprogram "$message" "$interval"
done < "$inputfile"
bash text-processing
 |Â
show 2 more comments
up vote
1
down vote
favorite
How can I read a line from a file into two variables: one variable for the last field, and the the other variable for the other fields?
For example, I have a file:
hello world! 10s
It is a good day. 4m
...
I would like to read each line into two variables: one variable contains the time interval at the end of the line, and the other variable contains the fields before it. I was wondering how to do it; this is as far as I have gotten:
while read line
do
... # split $line into $message and $interval
myprogram "$message" "$interval"
done < "$inputfile"
bash text-processing
Will the interval ever contain white space?
â Jesse_b
7 hours ago
Thanks. The time interval will be used as argument tosleep
. Ifsleep
accepts an argument with a space within it, then yes; otherwise, no.
â Tim
7 hours ago
Obligatory unix.stackexchange.com/q/169716/117549
â Jeff Schaller
7 hours ago
@JeffSchaller Could you tell me how I should rewrite my while loop? I have looked at the link
â Tim
6 hours ago
It's apparent that reading into variables is not your final destination; there might be other/better ways to accomplish your goal, if we knew what it was.
â Jeff Schaller
6 hours ago
 |Â
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How can I read a line from a file into two variables: one variable for the last field, and the the other variable for the other fields?
For example, I have a file:
hello world! 10s
It is a good day. 4m
...
I would like to read each line into two variables: one variable contains the time interval at the end of the line, and the other variable contains the fields before it. I was wondering how to do it; this is as far as I have gotten:
while read line
do
... # split $line into $message and $interval
myprogram "$message" "$interval"
done < "$inputfile"
bash text-processing
How can I read a line from a file into two variables: one variable for the last field, and the the other variable for the other fields?
For example, I have a file:
hello world! 10s
It is a good day. 4m
...
I would like to read each line into two variables: one variable contains the time interval at the end of the line, and the other variable contains the fields before it. I was wondering how to do it; this is as far as I have gotten:
while read line
do
... # split $line into $message and $interval
myprogram "$message" "$interval"
done < "$inputfile"
bash text-processing
bash text-processing
edited 4 mins ago
K7AAY
236319
236319
asked 8 hours ago
Tim
24.7k70239429
24.7k70239429
Will the interval ever contain white space?
â Jesse_b
7 hours ago
Thanks. The time interval will be used as argument tosleep
. Ifsleep
accepts an argument with a space within it, then yes; otherwise, no.
â Tim
7 hours ago
Obligatory unix.stackexchange.com/q/169716/117549
â Jeff Schaller
7 hours ago
@JeffSchaller Could you tell me how I should rewrite my while loop? I have looked at the link
â Tim
6 hours ago
It's apparent that reading into variables is not your final destination; there might be other/better ways to accomplish your goal, if we knew what it was.
â Jeff Schaller
6 hours ago
 |Â
show 2 more comments
Will the interval ever contain white space?
â Jesse_b
7 hours ago
Thanks. The time interval will be used as argument tosleep
. Ifsleep
accepts an argument with a space within it, then yes; otherwise, no.
â Tim
7 hours ago
Obligatory unix.stackexchange.com/q/169716/117549
â Jeff Schaller
7 hours ago
@JeffSchaller Could you tell me how I should rewrite my while loop? I have looked at the link
â Tim
6 hours ago
It's apparent that reading into variables is not your final destination; there might be other/better ways to accomplish your goal, if we knew what it was.
â Jeff Schaller
6 hours ago
Will the interval ever contain white space?
â Jesse_b
7 hours ago
Will the interval ever contain white space?
â Jesse_b
7 hours ago
Thanks. The time interval will be used as argument to
sleep
. If sleep
accepts an argument with a space within it, then yes; otherwise, no.â Tim
7 hours ago
Thanks. The time interval will be used as argument to
sleep
. If sleep
accepts an argument with a space within it, then yes; otherwise, no.â Tim
7 hours ago
Obligatory unix.stackexchange.com/q/169716/117549
â Jeff Schaller
7 hours ago
Obligatory unix.stackexchange.com/q/169716/117549
â Jeff Schaller
7 hours ago
@JeffSchaller Could you tell me how I should rewrite my while loop? I have looked at the link
â Tim
6 hours ago
@JeffSchaller Could you tell me how I should rewrite my while loop? I have looked at the link
â Tim
6 hours ago
It's apparent that reading into variables is not your final destination; there might be other/better ways to accomplish your goal, if we knew what it was.
â Jeff Schaller
6 hours ago
It's apparent that reading into variables is not your final destination; there might be other/better ways to accomplish your goal, if we knew what it was.
â Jeff Schaller
6 hours ago
 |Â
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
As long as the interval contains no whitespace this should work:
#!/bin/bash
input=/path/to/input
while read -r line; do
message=$line% *
interval=$line##*
echo "$message"
sleep "$interval"
done < "$input"
$line% *
Will strip everything after the last space character
$line##*
Will strip everything before the last space character
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
As long as the interval contains no whitespace this should work:
#!/bin/bash
input=/path/to/input
while read -r line; do
message=$line% *
interval=$line##*
echo "$message"
sleep "$interval"
done < "$input"
$line% *
Will strip everything after the last space character
$line##*
Will strip everything before the last space character
add a comment |Â
up vote
3
down vote
accepted
As long as the interval contains no whitespace this should work:
#!/bin/bash
input=/path/to/input
while read -r line; do
message=$line% *
interval=$line##*
echo "$message"
sleep "$interval"
done < "$input"
$line% *
Will strip everything after the last space character
$line##*
Will strip everything before the last space character
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
As long as the interval contains no whitespace this should work:
#!/bin/bash
input=/path/to/input
while read -r line; do
message=$line% *
interval=$line##*
echo "$message"
sleep "$interval"
done < "$input"
$line% *
Will strip everything after the last space character
$line##*
Will strip everything before the last space character
As long as the interval contains no whitespace this should work:
#!/bin/bash
input=/path/to/input
while read -r line; do
message=$line% *
interval=$line##*
echo "$message"
sleep "$interval"
done < "$input"
$line% *
Will strip everything after the last space character
$line##*
Will strip everything before the last space character
answered 7 hours ago
Jesse_b
10.6k22661
10.6k22661
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480663%2fhow-can-i-read-each-line-from-a-file-into-one-variable-for-the-last-field-and-a%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Will the interval ever contain white space?
â Jesse_b
7 hours ago
Thanks. The time interval will be used as argument to
sleep
. Ifsleep
accepts an argument with a space within it, then yes; otherwise, no.â Tim
7 hours ago
Obligatory unix.stackexchange.com/q/169716/117549
â Jeff Schaller
7 hours ago
@JeffSchaller Could you tell me how I should rewrite my while loop? I have looked at the link
â Tim
6 hours ago
It's apparent that reading into variables is not your final destination; there might be other/better ways to accomplish your goal, if we knew what it was.
â Jeff Schaller
6 hours ago