Counting the number of lines between same repeating pattern

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












0














I have refer this Grep the lines between the occurrence of the same pattern , I don't want to split into file; instead, I want to store it into an array. File count.txt contain:



0 
1
2
3
0
1
2
0
1


My script code is:



total=$(sed -n $= count.txt)
c=0
k=0
lineno=0
var="0"
for i in $(cat count.txt);
do
if ["$i" -eq "$var"]
then
arr[$((k++))]=c
c=0
c=$((c+1))

else
c=$((c+1))
if ["$lineno" -eq "$total"]
then
arr[$((k++))]=c
fi
fi
lineno=$((lineno+1))
done


The above logic is tested on C++ launguage which on printing the array shows : 0 4 3 2
Firstly, my script is showing error like line 9: [0: command not found.
Secondly, is there any efficient method to store the repeated count in array, like above output?










share|improve this question



















  • 1




    The command not found error is because your [ test lacks the required whitespace: [ "$i" -eq "$var" ]
    – steeldriver
    Dec 27 '18 at 13:06










  • @msp9011 I want the output: 4 3 2 in array
    – Arya
    Dec 27 '18 at 13:17










  • There is not output instruction; in addition =c is putting the letter c in the variable, may be you want =$c and the first test is right the first time as 0=0 this explains the potential output 0 in sequence instead of 4 3 2.
    – matzeri
    Dec 27 '18 at 14:22
















0














I have refer this Grep the lines between the occurrence of the same pattern , I don't want to split into file; instead, I want to store it into an array. File count.txt contain:



0 
1
2
3
0
1
2
0
1


My script code is:



total=$(sed -n $= count.txt)
c=0
k=0
lineno=0
var="0"
for i in $(cat count.txt);
do
if ["$i" -eq "$var"]
then
arr[$((k++))]=c
c=0
c=$((c+1))

else
c=$((c+1))
if ["$lineno" -eq "$total"]
then
arr[$((k++))]=c
fi
fi
lineno=$((lineno+1))
done


The above logic is tested on C++ launguage which on printing the array shows : 0 4 3 2
Firstly, my script is showing error like line 9: [0: command not found.
Secondly, is there any efficient method to store the repeated count in array, like above output?










share|improve this question



















  • 1




    The command not found error is because your [ test lacks the required whitespace: [ "$i" -eq "$var" ]
    – steeldriver
    Dec 27 '18 at 13:06










  • @msp9011 I want the output: 4 3 2 in array
    – Arya
    Dec 27 '18 at 13:17










  • There is not output instruction; in addition =c is putting the letter c in the variable, may be you want =$c and the first test is right the first time as 0=0 this explains the potential output 0 in sequence instead of 4 3 2.
    – matzeri
    Dec 27 '18 at 14:22














0












0








0







I have refer this Grep the lines between the occurrence of the same pattern , I don't want to split into file; instead, I want to store it into an array. File count.txt contain:



0 
1
2
3
0
1
2
0
1


My script code is:



total=$(sed -n $= count.txt)
c=0
k=0
lineno=0
var="0"
for i in $(cat count.txt);
do
if ["$i" -eq "$var"]
then
arr[$((k++))]=c
c=0
c=$((c+1))

else
c=$((c+1))
if ["$lineno" -eq "$total"]
then
arr[$((k++))]=c
fi
fi
lineno=$((lineno+1))
done


The above logic is tested on C++ launguage which on printing the array shows : 0 4 3 2
Firstly, my script is showing error like line 9: [0: command not found.
Secondly, is there any efficient method to store the repeated count in array, like above output?










share|improve this question















I have refer this Grep the lines between the occurrence of the same pattern , I don't want to split into file; instead, I want to store it into an array. File count.txt contain:



0 
1
2
3
0
1
2
0
1


My script code is:



total=$(sed -n $= count.txt)
c=0
k=0
lineno=0
var="0"
for i in $(cat count.txt);
do
if ["$i" -eq "$var"]
then
arr[$((k++))]=c
c=0
c=$((c+1))

else
c=$((c+1))
if ["$lineno" -eq "$total"]
then
arr[$((k++))]=c
fi
fi
lineno=$((lineno+1))
done


The above logic is tested on C++ launguage which on printing the array shows : 0 4 3 2
Firstly, my script is showing error like line 9: [0: command not found.
Secondly, is there any efficient method to store the repeated count in array, like above output?







bash awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

39.3k1479131




39.3k1479131










asked Dec 27 '18 at 13:01









AryaArya

1




1







  • 1




    The command not found error is because your [ test lacks the required whitespace: [ "$i" -eq "$var" ]
    – steeldriver
    Dec 27 '18 at 13:06










  • @msp9011 I want the output: 4 3 2 in array
    – Arya
    Dec 27 '18 at 13:17










  • There is not output instruction; in addition =c is putting the letter c in the variable, may be you want =$c and the first test is right the first time as 0=0 this explains the potential output 0 in sequence instead of 4 3 2.
    – matzeri
    Dec 27 '18 at 14:22













  • 1




    The command not found error is because your [ test lacks the required whitespace: [ "$i" -eq "$var" ]
    – steeldriver
    Dec 27 '18 at 13:06










  • @msp9011 I want the output: 4 3 2 in array
    – Arya
    Dec 27 '18 at 13:17










  • There is not output instruction; in addition =c is putting the letter c in the variable, may be you want =$c and the first test is right the first time as 0=0 this explains the potential output 0 in sequence instead of 4 3 2.
    – matzeri
    Dec 27 '18 at 14:22








1




1




The command not found error is because your [ test lacks the required whitespace: [ "$i" -eq "$var" ]
– steeldriver
Dec 27 '18 at 13:06




The command not found error is because your [ test lacks the required whitespace: [ "$i" -eq "$var" ]
– steeldriver
Dec 27 '18 at 13:06












@msp9011 I want the output: 4 3 2 in array
– Arya
Dec 27 '18 at 13:17




@msp9011 I want the output: 4 3 2 in array
– Arya
Dec 27 '18 at 13:17












There is not output instruction; in addition =c is putting the letter c in the variable, may be you want =$c and the first test is right the first time as 0=0 this explains the potential output 0 in sequence instead of 4 3 2.
– matzeri
Dec 27 '18 at 14:22





There is not output instruction; in addition =c is putting the letter c in the variable, may be you want =$c and the first test is right the first time as 0=0 this explains the potential output 0 in sequence instead of 4 3 2.
– matzeri
Dec 27 '18 at 14:22











2 Answers
2






active

oldest

votes


















1














An awk one-liner:



awk -v patt="0" -v prev=1 '
$0 ~ patt print NR - prev; prev = NR
END print NR + 1 - prev
' file





share|improve this answer




























    0














    Try this,



    LINES=(`cat count.txt `)
    arr=()
    arrIndex=0
    a=`echo $LINES`
    for i in "$!LINES[@]"; do
    if [[ "$LINES[$i]" = "$a" ]]; then
    arr[arrIndex]=`echo "$i"`
    arrIndex=$((arrIndex+1))
    fi
    done
    arr[arrIndex]=`echo $#LINES[@]`
    arrOut=()
    for ((i=1; i<"$#arr[@]"; ++i))
    do
    j=`expr $i - 1 `
    arrOut[$i]=`echo "$arr[$i] $arr[$j]" | awk 'a=$1-$2;print a'`
    done

    echo $arrOut[@]





    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',
      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%2f491120%2fcounting-the-number-of-lines-between-same-repeating-pattern%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









      1














      An awk one-liner:



      awk -v patt="0" -v prev=1 '
      $0 ~ patt print NR - prev; prev = NR
      END print NR + 1 - prev
      ' file





      share|improve this answer

























        1














        An awk one-liner:



        awk -v patt="0" -v prev=1 '
        $0 ~ patt print NR - prev; prev = NR
        END print NR + 1 - prev
        ' file





        share|improve this answer























          1












          1








          1






          An awk one-liner:



          awk -v patt="0" -v prev=1 '
          $0 ~ patt print NR - prev; prev = NR
          END print NR + 1 - prev
          ' file





          share|improve this answer












          An awk one-liner:



          awk -v patt="0" -v prev=1 '
          $0 ~ patt print NR - prev; prev = NR
          END print NR + 1 - prev
          ' file






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 27 '18 at 15:07









          glenn jackmanglenn jackman

          50.4k570107




          50.4k570107























              0














              Try this,



              LINES=(`cat count.txt `)
              arr=()
              arrIndex=0
              a=`echo $LINES`
              for i in "$!LINES[@]"; do
              if [[ "$LINES[$i]" = "$a" ]]; then
              arr[arrIndex]=`echo "$i"`
              arrIndex=$((arrIndex+1))
              fi
              done
              arr[arrIndex]=`echo $#LINES[@]`
              arrOut=()
              for ((i=1; i<"$#arr[@]"; ++i))
              do
              j=`expr $i - 1 `
              arrOut[$i]=`echo "$arr[$i] $arr[$j]" | awk 'a=$1-$2;print a'`
              done

              echo $arrOut[@]





              share|improve this answer

























                0














                Try this,



                LINES=(`cat count.txt `)
                arr=()
                arrIndex=0
                a=`echo $LINES`
                for i in "$!LINES[@]"; do
                if [[ "$LINES[$i]" = "$a" ]]; then
                arr[arrIndex]=`echo "$i"`
                arrIndex=$((arrIndex+1))
                fi
                done
                arr[arrIndex]=`echo $#LINES[@]`
                arrOut=()
                for ((i=1; i<"$#arr[@]"; ++i))
                do
                j=`expr $i - 1 `
                arrOut[$i]=`echo "$arr[$i] $arr[$j]" | awk 'a=$1-$2;print a'`
                done

                echo $arrOut[@]





                share|improve this answer























                  0












                  0








                  0






                  Try this,



                  LINES=(`cat count.txt `)
                  arr=()
                  arrIndex=0
                  a=`echo $LINES`
                  for i in "$!LINES[@]"; do
                  if [[ "$LINES[$i]" = "$a" ]]; then
                  arr[arrIndex]=`echo "$i"`
                  arrIndex=$((arrIndex+1))
                  fi
                  done
                  arr[arrIndex]=`echo $#LINES[@]`
                  arrOut=()
                  for ((i=1; i<"$#arr[@]"; ++i))
                  do
                  j=`expr $i - 1 `
                  arrOut[$i]=`echo "$arr[$i] $arr[$j]" | awk 'a=$1-$2;print a'`
                  done

                  echo $arrOut[@]





                  share|improve this answer












                  Try this,



                  LINES=(`cat count.txt `)
                  arr=()
                  arrIndex=0
                  a=`echo $LINES`
                  for i in "$!LINES[@]"; do
                  if [[ "$LINES[$i]" = "$a" ]]; then
                  arr[arrIndex]=`echo "$i"`
                  arrIndex=$((arrIndex+1))
                  fi
                  done
                  arr[arrIndex]=`echo $#LINES[@]`
                  arrOut=()
                  for ((i=1; i<"$#arr[@]"; ++i))
                  do
                  j=`expr $i - 1 `
                  arrOut[$i]=`echo "$arr[$i] $arr[$j]" | awk 'a=$1-$2;print a'`
                  done

                  echo $arrOut[@]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 27 '18 at 14:13









                  msp9011msp9011

                  3,82843863




                  3,82843863



























                      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%2f491120%2fcounting-the-number-of-lines-between-same-repeating-pattern%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