Unix script terminates after reading first line in file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am trying to count the number of tabs per line in file and when condition matches print the line to another file. But the script reads only the first line and terminates.



Please let me know on what's wrong in the below,



#!/bin/bash
set -e
set -o pipefail

filename="0101.tsv"
while IFS= read -r line;do

s=$(awk 'print gsub(/t/,"")')
echo $s

if [[ $s -eq 995 ]]; then
printf "%sn" "$line"
continue
fi

done < $filename > abc.tsv


Thanks!.










share|improve this question

















  • 5




    You are not providing any input to awk. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g. awk 'gsub(/t/,"") == 995 print' 0101.tsv or (assuming you want to count tab separated fields) simply awk -F't' 'NF == 996' 0101.tsv
    – steeldriver
    Dec 10 at 7:37











  • @steeldriver You should probably make this an answer instead.
    – Graipher
    Dec 10 at 9:37














up vote
1
down vote

favorite












I am trying to count the number of tabs per line in file and when condition matches print the line to another file. But the script reads only the first line and terminates.



Please let me know on what's wrong in the below,



#!/bin/bash
set -e
set -o pipefail

filename="0101.tsv"
while IFS= read -r line;do

s=$(awk 'print gsub(/t/,"")')
echo $s

if [[ $s -eq 995 ]]; then
printf "%sn" "$line"
continue
fi

done < $filename > abc.tsv


Thanks!.










share|improve this question

















  • 5




    You are not providing any input to awk. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g. awk 'gsub(/t/,"") == 995 print' 0101.tsv or (assuming you want to count tab separated fields) simply awk -F't' 'NF == 996' 0101.tsv
    – steeldriver
    Dec 10 at 7:37











  • @steeldriver You should probably make this an answer instead.
    – Graipher
    Dec 10 at 9:37












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to count the number of tabs per line in file and when condition matches print the line to another file. But the script reads only the first line and terminates.



Please let me know on what's wrong in the below,



#!/bin/bash
set -e
set -o pipefail

filename="0101.tsv"
while IFS= read -r line;do

s=$(awk 'print gsub(/t/,"")')
echo $s

if [[ $s -eq 995 ]]; then
printf "%sn" "$line"
continue
fi

done < $filename > abc.tsv


Thanks!.










share|improve this question













I am trying to count the number of tabs per line in file and when condition matches print the line to another file. But the script reads only the first line and terminates.



Please let me know on what's wrong in the below,



#!/bin/bash
set -e
set -o pipefail

filename="0101.tsv"
while IFS= read -r line;do

s=$(awk 'print gsub(/t/,"")')
echo $s

if [[ $s -eq 995 ]]; then
printf "%sn" "$line"
continue
fi

done < $filename > abc.tsv


Thanks!.







bash shell-script






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 10 at 7:12









v.rajan

93




93







  • 5




    You are not providing any input to awk. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g. awk 'gsub(/t/,"") == 995 print' 0101.tsv or (assuming you want to count tab separated fields) simply awk -F't' 'NF == 996' 0101.tsv
    – steeldriver
    Dec 10 at 7:37











  • @steeldriver You should probably make this an answer instead.
    – Graipher
    Dec 10 at 9:37












  • 5




    You are not providing any input to awk. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g. awk 'gsub(/t/,"") == 995 print' 0101.tsv or (assuming you want to count tab separated fields) simply awk -F't' 'NF == 996' 0101.tsv
    – steeldriver
    Dec 10 at 7:37











  • @steeldriver You should probably make this an answer instead.
    – Graipher
    Dec 10 at 9:37







5




5




You are not providing any input to awk. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g. awk 'gsub(/t/,"") == 995 print' 0101.tsv or (assuming you want to count tab separated fields) simply awk -F't' 'NF == 996' 0101.tsv
– steeldriver
Dec 10 at 7:37





You are not providing any input to awk. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g. awk 'gsub(/t/,"") == 995 print' 0101.tsv or (assuming you want to count tab separated fields) simply awk -F't' 'NF == 996' 0101.tsv
– steeldriver
Dec 10 at 7:37













@steeldriver You should probably make this an answer instead.
– Graipher
Dec 10 at 9:37




@steeldriver You should probably make this an answer instead.
– Graipher
Dec 10 at 9:37










2 Answers
2






active

oldest

votes

















up vote
2
down vote













You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.



If you want to print all lines that has 996 fields (995 tabs):



awk -F 't' 'NF == 996' <file


This is a shorthand way of writing



awk 'BEGIN FS = "t" NF == 996 print ' <file


where print means print $0, i.e. print the input record (line), and FS is the input field separator.



Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a single time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.






share|improve this answer





























    up vote
    0
    down vote













    The oneliners with awk by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,



    #!/bin/bash

    set -e
    set -o pipefail

    filename="0101.tsv"
    while IFS= read -r line
    do
    s=0
    len=$#line
    # echo "line=$line"
    # echo "len=$len"
    for (( i=0; i<$len; i++ ))
    do
    if [ "$line:i:1" == $'t' ]
    then
    s=$((s +1))
    fi
    done
    echo $s

    if [[ "$s" == "995" ]]; then
    printf "%sn" "$line"
    continue
    fi
    done < "$filename" > abc.tsv





    share|improve this answer
















    • 2




      Related: Why is using a shell loop to process text considered bad practice?
      – Kusalananda
      Dec 10 at 8:52










    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',
    autoActivateHeartbeat: false,
    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%2f487059%2funix-script-terminates-after-reading-first-line-in-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.



    If you want to print all lines that has 996 fields (995 tabs):



    awk -F 't' 'NF == 996' <file


    This is a shorthand way of writing



    awk 'BEGIN FS = "t" NF == 996 print ' <file


    where print means print $0, i.e. print the input record (line), and FS is the input field separator.



    Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a single time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.






    share|improve this answer


























      up vote
      2
      down vote













      You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.



      If you want to print all lines that has 996 fields (995 tabs):



      awk -F 't' 'NF == 996' <file


      This is a shorthand way of writing



      awk 'BEGIN FS = "t" NF == 996 print ' <file


      where print means print $0, i.e. print the input record (line), and FS is the input field separator.



      Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a single time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.






      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.



        If you want to print all lines that has 996 fields (995 tabs):



        awk -F 't' 'NF == 996' <file


        This is a shorthand way of writing



        awk 'BEGIN FS = "t" NF == 996 print ' <file


        where print means print $0, i.e. print the input record (line), and FS is the input field separator.



        Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a single time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.






        share|improve this answer














        You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.



        If you want to print all lines that has 996 fields (995 tabs):



        awk -F 't' 'NF == 996' <file


        This is a shorthand way of writing



        awk 'BEGIN FS = "t" NF == 996 print ' <file


        where print means print $0, i.e. print the input record (line), and FS is the input field separator.



        Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a single time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 10 at 10:09

























        answered Dec 10 at 10:02









        Kusalananda

        121k16226370




        121k16226370






















            up vote
            0
            down vote













            The oneliners with awk by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,



            #!/bin/bash

            set -e
            set -o pipefail

            filename="0101.tsv"
            while IFS= read -r line
            do
            s=0
            len=$#line
            # echo "line=$line"
            # echo "len=$len"
            for (( i=0; i<$len; i++ ))
            do
            if [ "$line:i:1" == $'t' ]
            then
            s=$((s +1))
            fi
            done
            echo $s

            if [[ "$s" == "995" ]]; then
            printf "%sn" "$line"
            continue
            fi
            done < "$filename" > abc.tsv





            share|improve this answer
















            • 2




              Related: Why is using a shell loop to process text considered bad practice?
              – Kusalananda
              Dec 10 at 8:52














            up vote
            0
            down vote













            The oneliners with awk by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,



            #!/bin/bash

            set -e
            set -o pipefail

            filename="0101.tsv"
            while IFS= read -r line
            do
            s=0
            len=$#line
            # echo "line=$line"
            # echo "len=$len"
            for (( i=0; i<$len; i++ ))
            do
            if [ "$line:i:1" == $'t' ]
            then
            s=$((s +1))
            fi
            done
            echo $s

            if [[ "$s" == "995" ]]; then
            printf "%sn" "$line"
            continue
            fi
            done < "$filename" > abc.tsv





            share|improve this answer
















            • 2




              Related: Why is using a shell loop to process text considered bad practice?
              – Kusalananda
              Dec 10 at 8:52












            up vote
            0
            down vote










            up vote
            0
            down vote









            The oneliners with awk by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,



            #!/bin/bash

            set -e
            set -o pipefail

            filename="0101.tsv"
            while IFS= read -r line
            do
            s=0
            len=$#line
            # echo "line=$line"
            # echo "len=$len"
            for (( i=0; i<$len; i++ ))
            do
            if [ "$line:i:1" == $'t' ]
            then
            s=$((s +1))
            fi
            done
            echo $s

            if [[ "$s" == "995" ]]; then
            printf "%sn" "$line"
            continue
            fi
            done < "$filename" > abc.tsv





            share|improve this answer












            The oneliners with awk by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,



            #!/bin/bash

            set -e
            set -o pipefail

            filename="0101.tsv"
            while IFS= read -r line
            do
            s=0
            len=$#line
            # echo "line=$line"
            # echo "len=$len"
            for (( i=0; i<$len; i++ ))
            do
            if [ "$line:i:1" == $'t' ]
            then
            s=$((s +1))
            fi
            done
            echo $s

            if [[ "$s" == "995" ]]; then
            printf "%sn" "$line"
            continue
            fi
            done < "$filename" > abc.tsv






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 10 at 8:36









            sudodus

            82616




            82616







            • 2




              Related: Why is using a shell loop to process text considered bad practice?
              – Kusalananda
              Dec 10 at 8:52












            • 2




              Related: Why is using a shell loop to process text considered bad practice?
              – Kusalananda
              Dec 10 at 8:52







            2




            2




            Related: Why is using a shell loop to process text considered bad practice?
            – Kusalananda
            Dec 10 at 8:52




            Related: Why is using a shell loop to process text considered bad practice?
            – Kusalananda
            Dec 10 at 8:52

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


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




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487059%2funix-script-terminates-after-reading-first-line-in-file%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

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay