Ubuntu: Count lines between “X”s


I want to count the lines between "X"s. This is just an example; I have to apply the code to a complex biological result. I will be thankful if you can suggest some command, preferably using awk, grep or sed as I am familiar with those.


X  Y  Y  Y  X  Y  Y  Y  Y  X  Y  X  

Desired Output:

3  4  1  


With awk:

$ awk '!/X/{count++}/X/{print count; count = 0}' input    3  4  1  

Increment a count for every line not containing X; print and reset the count for lines containing X.


$ awk '/X/ && prev{print NR-prev-1} /X/{prev=NR}' file  3  4  1  

How it works:

Awk implicitly reads through input files line by line.

  • /X/ && prev{print NR-prev-1}

    For any line that contains X and if we have previously assigned a value to prev, then print out the number of the current line, NR, minus prev minus one.

  • /X/{prev=NR}

    For any line that contains X, set the variable prev to the current line number, NR.


Another simple awk approach which works on OP's sample data and if X was not in first or even in last or repeated Xs.

awk -v RS='X' 'NF{print NF}' infile  

Above is correct when there is only one field in each line with default FS any whitespaces, otherwise below is revised in general case for counting linewise. You can input your PATTERN in place of X there.

awk -F'\n' -v RS='X' 'NF>2{print NF-2}'  

Sample input:

X  Y YYY Y  YY  YY Y YY YY Y Y  X  Y Y Y  X  Y  Y  X  X  

The output is:

3  1  2  


Most of the answers here match the content of the line to be counted using regular expressions embedded into the Awk program. If you need to match lines with content that may contain special characters (either to Awk or regular expressions) it would be better to actually compare strings for equality. Therefore I propose the following Awk script as a variant of muru’s answer:

BEGIN {      count = 0;  }    {      if ($0 == needle) {          if (count) {              print count;              count = 0;          }      } else {          count++;      }  }  

Store it as a text file, e. g. count-rows.awk, and invoke it as follows:

awk -f count-rows.awk -v needle=X input  

You can adjust the value of needle to your liking. The advantage of this method is that you can invoke the program from a shell script with an arbitrary value for needle without escaping issues:

awk -f count-rows.awk -v needle="$needle" input  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »