Count the Consecutive Occurance of "X" in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the Consecutive Occurance of "X" in awk
# 1  
Old 11-09-2008
Count the Consecutive Occurance of "X" in awk

Hi All,

I have a data as follow:

0
0
0
X
X
0
X
X
X
0
X
0
0
X
0
0
....
....


I want to count how many times the consecutive occurrence of Xs. As

000 XX0 XXX0 X0 0 X0 0000 X0 XX0 000 XXXX0 X0 XX0 X0
-----1----2----3----1-------1---2---------1----2--3---4 (I want to print out this)

Please, let me know how to write an awk code for this..

Thanks

Last edited by nica; 11-09-2008 at 10:50 AM..
# 2  
Old 11-09-2008
I don't see the logical to get the desired output with the given data.
Why don't you show us the code you have so far, and tell us how it misbehaves?
# 3  
Old 11-09-2008
Hi Franklin52,


Thanks for the quick respond..


Ok, I try to explain more details....



If every X followed by 0 is count as occurrence 1
e.g: X0 , XX0, XXX0, XXXX0, so on... count as occurrence 1


For example, if there is a range of data like this:
000X0X0000X0XX0XXX0XX00000X000


then, we can distinguishes like this:

000 X0X0 000 X0XX0XXX0XX0 0000 XXXXX0 00

X0X0 = count as occurrence 2 (there are two X0s)

X0XX0XXX0XX0 = count as occurrence 4
There are X0, XX0, XXX0, XX0


XXXXX0 = count as occurrence 1



So, the desired output of example above is:
2
4
1



(Remember, The range of data is displayed in row by row)


I hope you can understand my explanation Smilie


Thanks
# 4  
Old 11-09-2008
Code:
echo "000 X0X0 000 X0XX0XXX0XX0 0000 XXXXX0 00" |perl -ne 'foreach(split){
@a=m/X0/g;
print $#a+1,"-";
}'

Code:
0-2-0-4-0-1-0-

# 5  
Old 11-10-2008
Hi Summer Cherry,

Thanks for the Perl Code..Smilie

Do you have any idea how to write it in Awk?
FYI, my Input Data range is displayed line by line.

ThanksSmilie
# 6  
Old 11-10-2008
Try this awk:
Code:
echo "000 X0X0 000 X0XX0XXX0XX0 0000 XXXXX0 00"|awk '{print NF-1}' RS=" " FS="X0" ORS="-"

# 7  
Old 11-10-2008
Another one, with line number before the numbers of occurrences:

Code:
awk '{
  print "Line : " NR
  for(i=1;i<=NF;i++){
    n=gsub("X0","",$i)
    if(n){print n}
  }
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Awk-sed - wc : how to count DOTS "."'s in a file.

Hi Experts , file: EST 2013::.................................................................................................................................................................................................................................................cmihx021:/home/data1/ ... (11 Replies)
Discussion started by: rveri
11 Replies

3. Post Here to Contact Site Administrators and Moderators

Suggestion: adding two new groups "sed" and "awk"

Majority of the questions are pertaining file/string parsing w.r.t sed or awk It would be nice to have these two as their own sub category under shell-programming-scripting which can avoid lot of duplicate posts. (1 Reply)
Discussion started by: jville
1 Replies

4. Linux

Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs:wall:, to find this soulution:- I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (=... (13 Replies)
Discussion started by: RedRocks!!
13 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

need to retrieve data between the first occurance of "_" and the extension.

hi all, i have a file that contains the following kind of data codeexpert_package_module1.html codeexpert_package_module2.html codeexpert_package_module3_revision2.html and it goes on .. i need to get the following data from it package_module1 package_module2 i know basename... (4 Replies)
Discussion started by: sais
4 Replies

9. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question