Counting the number of lines between same repeating pattern
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
1
Thecommand 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 as0=0
this explains the potential output 0 in sequence instead of4 3 2
.
– matzeri
Dec 27 '18 at 14:22
add a comment |
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
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
bash awk sed
edited 2 days ago
Rui F Ribeiro
39.3k1479131
39.3k1479131
asked Dec 27 '18 at 13:01
AryaArya
1
1
1
Thecommand 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 as0=0
this explains the potential output 0 in sequence instead of4 3 2
.
– matzeri
Dec 27 '18 at 14:22
add a comment |
1
Thecommand 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 as0=0
this explains the potential output 0 in sequence instead of4 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
add a comment |
2 Answers
2
active
oldest
votes
An awk one-liner:
awk -v patt="0" -v prev=1 '
$0 ~ patt print NR - prev; prev = NR
END print NR + 1 - prev
' file
add a comment |
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[@]
add a comment |
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
);
);
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
Required, but never shown
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
An awk one-liner:
awk -v patt="0" -v prev=1 '
$0 ~ patt print NR - prev; prev = NR
END print NR + 1 - prev
' file
add a comment |
An awk one-liner:
awk -v patt="0" -v prev=1 '
$0 ~ patt print NR - prev; prev = NR
END print NR + 1 - prev
' file
add a comment |
An awk one-liner:
awk -v patt="0" -v prev=1 '
$0 ~ patt print NR - prev; prev = NR
END print NR + 1 - prev
' file
An awk one-liner:
awk -v patt="0" -v prev=1 '
$0 ~ patt print NR - prev; prev = NR
END print NR + 1 - prev
' file
answered Dec 27 '18 at 15:07
glenn jackmanglenn jackman
50.4k570107
50.4k570107
add a comment |
add a comment |
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[@]
add a comment |
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[@]
add a comment |
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[@]
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[@]
answered Dec 27 '18 at 14:13
msp9011msp9011
3,82843863
3,82843863
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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 as0=0
this explains the potential output 0 in sequence instead of4 3 2
.– matzeri
Dec 27 '18 at 14:22