Bash script to read line and increment











up vote
0
down vote

favorite












I have a file that has the following format



1|3
7|10
11|16


and I would like with a script to have the following format



1
2
3
7
8
9
10
11
12
13
14
15
16


Basically I have a range and in the first column is the start number and in the second column the end number and I would liked to have the output file with all the numbers from that range in a new line .










share|improve this question









New contributor




bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • There seems to be several questions here: Which bit are you stuck on?
    – ctrl-alt-delor
    Nov 22 at 9:54










  • I don't even have any kind of idea on how to start .
    – bboy
    Nov 22 at 10:39










  • Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
    – ctrl-alt-delor
    Nov 22 at 10:47

















up vote
0
down vote

favorite












I have a file that has the following format



1|3
7|10
11|16


and I would like with a script to have the following format



1
2
3
7
8
9
10
11
12
13
14
15
16


Basically I have a range and in the first column is the start number and in the second column the end number and I would liked to have the output file with all the numbers from that range in a new line .










share|improve this question









New contributor




bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • There seems to be several questions here: Which bit are you stuck on?
    – ctrl-alt-delor
    Nov 22 at 9:54










  • I don't even have any kind of idea on how to start .
    – bboy
    Nov 22 at 10:39










  • Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
    – ctrl-alt-delor
    Nov 22 at 10:47















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file that has the following format



1|3
7|10
11|16


and I would like with a script to have the following format



1
2
3
7
8
9
10
11
12
13
14
15
16


Basically I have a range and in the first column is the start number and in the second column the end number and I would liked to have the output file with all the numbers from that range in a new line .










share|improve this question









New contributor




bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a file that has the following format



1|3
7|10
11|16


and I would like with a script to have the following format



1
2
3
7
8
9
10
11
12
13
14
15
16


Basically I have a range and in the first column is the start number and in the second column the end number and I would liked to have the output file with all the numbers from that range in a new line .







bash shell






share|improve this question









New contributor




bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 22 at 10:28









Rui F Ribeiro

38.3k1475126




38.3k1475126






New contributor




bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 22 at 9:52









bboy

274




274




New contributor




bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






bboy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • There seems to be several questions here: Which bit are you stuck on?
    – ctrl-alt-delor
    Nov 22 at 9:54










  • I don't even have any kind of idea on how to start .
    – bboy
    Nov 22 at 10:39










  • Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
    – ctrl-alt-delor
    Nov 22 at 10:47




















  • There seems to be several questions here: Which bit are you stuck on?
    – ctrl-alt-delor
    Nov 22 at 9:54










  • I don't even have any kind of idea on how to start .
    – bboy
    Nov 22 at 10:39










  • Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
    – ctrl-alt-delor
    Nov 22 at 10:47


















There seems to be several questions here: Which bit are you stuck on?
– ctrl-alt-delor
Nov 22 at 9:54




There seems to be several questions here: Which bit are you stuck on?
– ctrl-alt-delor
Nov 22 at 9:54












I don't even have any kind of idea on how to start .
– bboy
Nov 22 at 10:39




I don't even have any kind of idea on how to start .
– bboy
Nov 22 at 10:39












Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
– ctrl-alt-delor
Nov 22 at 10:47






Can you read a file? Can you read it line by line? Can you read the individual fields? Can you construct a range from two numbers? Just do one of these. When it is working, add another, until done. How do you climb a tall building? One step at a time. If you try to jump, then you will fail.
– ctrl-alt-delor
Nov 22 at 10:47












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










With awk:



awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output





share|improve this answer























  • Perfect , thank you very much .
    – bboy
    Nov 22 at 10:40










  • This might be a tiny bit faster: {for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.
    – glenn jackman
    Nov 22 at 14:26










  • @glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the for loop is faster indeed, so I'm going to follow your recommendation.
    – Stéphane Chazelas
    Nov 22 at 14:37


















up vote
2
down vote













tr -s | " " < file.txt |xargs -l1 seq > output.txt





share|improve this answer





















  • Why the files?, stdin and stdout are more flexible.
    – ctrl-alt-delor
    Nov 22 at 9:55










  • In the question there was an input file and output also.
    – Ipor Sircer
    Nov 22 at 9:56


















up vote
0
down vote













Assuming the values are strictly valid base 10 numbers (if not, clean them):



$ cat ./script.sh
#!/bin/bash
while IFS='|' read a b
do until ((a>b))
do printf '%dn' "$((a++))"
done
done

$ ./script <infile >outfile





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
    });


    }
    });






    bboy is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483402%2fbash-script-to-read-line-and-increment%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    With awk:



    awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output





    share|improve this answer























    • Perfect , thank you very much .
      – bboy
      Nov 22 at 10:40










    • This might be a tiny bit faster: {for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.
      – glenn jackman
      Nov 22 at 14:26










    • @glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the for loop is faster indeed, so I'm going to follow your recommendation.
      – Stéphane Chazelas
      Nov 22 at 14:37















    up vote
    1
    down vote



    accepted










    With awk:



    awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output





    share|improve this answer























    • Perfect , thank you very much .
      – bboy
      Nov 22 at 10:40










    • This might be a tiny bit faster: {for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.
      – glenn jackman
      Nov 22 at 14:26










    • @glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the for loop is faster indeed, so I'm going to follow your recommendation.
      – Stéphane Chazelas
      Nov 22 at 14:37













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    With awk:



    awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output





    share|improve this answer














    With awk:



    awk -F'|' '{for (i = $1; i <= $2; i++) print i}' < input > output






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 at 14:38

























    answered Nov 22 at 10:03









    Stéphane Chazelas

    295k54556898




    295k54556898












    • Perfect , thank you very much .
      – bboy
      Nov 22 at 10:40










    • This might be a tiny bit faster: {for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.
      – glenn jackman
      Nov 22 at 14:26










    • @glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the for loop is faster indeed, so I'm going to follow your recommendation.
      – Stéphane Chazelas
      Nov 22 at 14:37


















    • Perfect , thank you very much .
      – bboy
      Nov 22 at 10:40










    • This might be a tiny bit faster: {for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.
      – glenn jackman
      Nov 22 at 14:26










    • @glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the for loop is faster indeed, so I'm going to follow your recommendation.
      – Stéphane Chazelas
      Nov 22 at 14:37
















    Perfect , thank you very much .
    – bboy
    Nov 22 at 10:40




    Perfect , thank you very much .
    – bboy
    Nov 22 at 10:40












    This might be a tiny bit faster: {for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.
    – glenn jackman
    Nov 22 at 14:26




    This might be a tiny bit faster: {for (i=$1; i<=$2; i++) print i} as it doesn't have to rebuild $0 for each iteration of the loop.
    – glenn jackman
    Nov 22 at 14:26












    @glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the for loop is faster indeed, so I'm going to follow your recommendation.
    – Stéphane Chazelas
    Nov 22 at 14:37




    @glennjackman. Thanks. You could argue that the NF, $0 don't need to be updated unless they are accessed, but in my tests with mawk and the one true awk, using the for loop is faster indeed, so I'm going to follow your recommendation.
    – Stéphane Chazelas
    Nov 22 at 14:37












    up vote
    2
    down vote













    tr -s | " " < file.txt |xargs -l1 seq > output.txt





    share|improve this answer





















    • Why the files?, stdin and stdout are more flexible.
      – ctrl-alt-delor
      Nov 22 at 9:55










    • In the question there was an input file and output also.
      – Ipor Sircer
      Nov 22 at 9:56















    up vote
    2
    down vote













    tr -s | " " < file.txt |xargs -l1 seq > output.txt





    share|improve this answer





















    • Why the files?, stdin and stdout are more flexible.
      – ctrl-alt-delor
      Nov 22 at 9:55










    • In the question there was an input file and output also.
      – Ipor Sircer
      Nov 22 at 9:56













    up vote
    2
    down vote










    up vote
    2
    down vote









    tr -s | " " < file.txt |xargs -l1 seq > output.txt





    share|improve this answer












    tr -s | " " < file.txt |xargs -l1 seq > output.txt






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 at 9:54









    Ipor Sircer

    10.2k11024




    10.2k11024












    • Why the files?, stdin and stdout are more flexible.
      – ctrl-alt-delor
      Nov 22 at 9:55










    • In the question there was an input file and output also.
      – Ipor Sircer
      Nov 22 at 9:56


















    • Why the files?, stdin and stdout are more flexible.
      – ctrl-alt-delor
      Nov 22 at 9:55










    • In the question there was an input file and output also.
      – Ipor Sircer
      Nov 22 at 9:56
















    Why the files?, stdin and stdout are more flexible.
    – ctrl-alt-delor
    Nov 22 at 9:55




    Why the files?, stdin and stdout are more flexible.
    – ctrl-alt-delor
    Nov 22 at 9:55












    In the question there was an input file and output also.
    – Ipor Sircer
    Nov 22 at 9:56




    In the question there was an input file and output also.
    – Ipor Sircer
    Nov 22 at 9:56










    up vote
    0
    down vote













    Assuming the values are strictly valid base 10 numbers (if not, clean them):



    $ cat ./script.sh
    #!/bin/bash
    while IFS='|' read a b
    do until ((a>b))
    do printf '%dn' "$((a++))"
    done
    done

    $ ./script <infile >outfile





    share|improve this answer

























      up vote
      0
      down vote













      Assuming the values are strictly valid base 10 numbers (if not, clean them):



      $ cat ./script.sh
      #!/bin/bash
      while IFS='|' read a b
      do until ((a>b))
      do printf '%dn' "$((a++))"
      done
      done

      $ ./script <infile >outfile





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Assuming the values are strictly valid base 10 numbers (if not, clean them):



        $ cat ./script.sh
        #!/bin/bash
        while IFS='|' read a b
        do until ((a>b))
        do printf '%dn' "$((a++))"
        done
        done

        $ ./script <infile >outfile





        share|improve this answer












        Assuming the values are strictly valid base 10 numbers (if not, clean them):



        $ cat ./script.sh
        #!/bin/bash
        while IFS='|' read a b
        do until ((a>b))
        do printf '%dn' "$((a++))"
        done
        done

        $ ./script <infile >outfile






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 0:33









        Isaac

        9,72811445




        9,72811445






















            bboy is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            bboy is a new contributor. Be nice, and check out our Code of Conduct.













            bboy is a new contributor. Be nice, and check out our Code of Conduct.












            bboy is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483402%2fbash-script-to-read-line-and-increment%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            List directoties down one level, excluding some named directories and files

            list processes belonging to a network namespace

            list systemd RuntimeDirectory mounts