How can I read each line from a file into one variable for the last field, and another variable for the other fields?

The name of the pictureThe name of the pictureThe name of the pictureClash 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"









share|improve this question























  • 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










  • 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














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"









share|improve this question























  • 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










  • 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












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"









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • @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










  • 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










  • @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










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






share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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
    );



    );













     

    draft saved


    draft discarded


















    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






























    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






    share|improve this answer
























      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






      share|improve this answer






















        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 7 hours ago









        Jesse_b

        10.6k22661




        10.6k22661



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay