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
# 8  
Old 11-10-2008
@Klashxx line modified for the OP requested output:
Code:
awk 'BEGIN{FS="X0";RS=" "} NF!=1{print NF-1}' filename


Last edited by danmero; 11-10-2008 at 11:03 AM..
# 9  
Old 11-10-2008
Thanks All for your help...

But all code which you posted does not work as what I expected. Please, see my desired output below.

I try to explain again more clearly..

If an X or consecutive Xs followed by 0 is count as occurrence 1
e.g: X0 , XX0, XXX0, XXXX0, ... count as occurrence 1



Here is my Input Data:
1 0
2 0
3 0
4 X
5 0
6 X
7 0
8 0
9 0
10 0
11 X
12 0
13 X
14 X
15 0
16 X
17 X
18 X
19 0
20 X
21 X
22 0
23 0
24 0
25 0
26 X
27 X
28 X
29 X
30 X
31 0
32 0
33 0

If it arrange in Line:
000X0X0000X0XX0XXX0XX0000XXXXX000





Then, we can distinguishes as follow:
000 X0X0 000 X0XX0XXX0XX0 0000 XXXXX0 00

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


X0XX0XXX0XX0 = count as occurrence 4

( X0-XX0-XXX0-XX0 = 4 Occurances )

XXXXX0 = count as occurrence 1

So, My Desired Output is:
2
4
1



I hope you can understand my explanation Smilie
# 10  
Old 11-10-2008
Have you tried my solution?

Code:
$ cat file
000 X0X0 000 X0XX0XXX0XX0 0000 XXXXX0 00
000 X0X0 0X0 X0XX0XXX0XX0 0000 XXXXX0 00
00X X0X0 000 X0XX0XXX0XX0 X000 XXXXX0 00
$
$
awk '{
  print "Line : " NR
  for(i=1;i<=NF;i++){
    n=gsub("X0","",$i)
    if(n){print n}
  }
}' file
Line : 1
2
4
1
Line : 2
2
1
4
1
Line : 3
2
4
1
1

If you don't want to output the line numbers remove the first print command.

Regards
# 11  
Old 11-10-2008
Thanks Franklin52,

Yes, I have tried your solution but the code did not work what I expected.


$ cat file
000 X0X0 000 X0XX0XXX0XX0 0000 XXXXX0 00
000 X0X0 0X0 X0XX0XXX0XX0 0000 XXXXX0 00
00X X0X0 000 X0XX0XXX0XX0 X000 XXXXX0 00

If I used your file input (above), my desired output should be as follow:
Line : 1
2
4
1
Line : 2
2
5
1
Line : 3
2
5
1


Explanations:


Line 1 is OK !



Line 2:

Line: 2 should be arranged: 000 X0X0 0 X0X0XX0XXX0XX0 0000 XXXXX0 00
X0X0 = X0, X0 = 2 Occurrences
X0X0XX0XXX0XX0 = X0, X0, XX0, XXX0, XX0 = 5 Occurrences
XXXXX0 = 1 Occurrence
Output of Line 2 should be:

2
5
1




Line 3:

Line: 3 should be arranged: 00 XX0X0 000 X0XX0XXX0XX0X0 00 XXXXX0 00
XX0X0= XX0, X0 = 2 Occurrences
X0XX0XXX0XX0X0= X0, XX0, XXX0, XX0, X0 = 5 Occurances
XXXXX0 = 1 Occurances
Output of Line 3 should be:
2
5
1




I hope you can catch what I want....Smilie


Thanks

Last edited by nica; 11-10-2008 at 11:56 AM..
# 12  
Old 11-10-2008
Quote:
Originally Posted by nica
I hope you can catch what I want....Smilie
You arrange the 3th field from the second position to the 4th field at line 2 and the 5th field to the 4th field at line 3. Sorry I don't understand the logic.

Maybe it should be helpful if you post your original file and the desired output of that file.

Regards
# 13  
Old 11-10-2008
Thanks Franklin52,

Ok, I think, distinguishes by field confuse us. (Actually, we dont need to distinguishes by field, this distinguishes is only separate the consecutive occurrence and non occurrences)

If we take the your input file again:
Line 2:
000X0X00X0X0XX0XXX0XX00000XXXXX000
then:
000X0X00X0X0XX0XXX0XX00000XXXXX000
2 5 1

Line 3:
00XX0X0000X0XX0XXX0XX0X000XXXXX000
then:
00XX0X0000X0XX0XXX0XX0X000XXXXX000
2 5 1

Again, If an X or consecutive Xs followed by 0 is count as occurrence 1
e.g: X0 , XX0, XXX0, XXXX0, … count as occurrence 1

Please, look the pattern carefully:

0000X0X0X000 = 3 Consecutive Occurrences (X0, X0, X0)
3

000XX0X0X000 = 3 Consecutive Occurrences (XX0, X0, X0 )
3

000X0XXX0X00 = 3 Consecutive Occurrences (X0, XXX0, X0 )
3

XX000X0X0000 = 1 Occurrence & 2 Consecutive Occurrences
1 2

X0X000X00X0XX0X00 = 2 Consec Occur & 1 Occur & 3 Consec Occur
2 1 3


Another Example:

If Input File ( this is one of my original input files):

1 0
2 0
3 X
4 X
5 X
6 X
7 0
8 X
9 0
10 0
11 0
12 0
13 0
14 X
15 0
16 0
17 0
18 X
19 X
20 X
21 0
22 0
23 0
24 0
25 0
26 X
27 X
28 0
29 X
30 X
31 X
32 X
33 0
34 X
35 0
36 0
37 0
38 X
39 0
40 X
41 0
42 X
43 0
44 X
45 0
46 X
47 X
48 0
49 0
50 0

If we arrange in Line ( Actually, we don't need to arrange in line when implemented in code, this is just make easy to understand what I want):
00XXXX0X00000X000XXX00000XX0XXXX0X000X0X0X0X0XX000

then we can determine the consecutive occurrence:

00XXXX0X00000X000XXX00000XX0XXXX0X000X0XXX0X0X0XX000
2 1 1 3 5


So, Desired output is:
2
1
1
3
5



Thanks,

Last edited by nica; 11-10-2008 at 11:24 PM..
# 14  
Old 11-11-2008
The following awk script gives the expected output for the sample 50 row file
Code:
BEGIN {state=count=0}
{
 if ( $2 == "X" ) {
    if (state == 0 || state == 2) { state=1; count++ }
 } else {
    if ( state == 2) { print count; state=count=0 }
    if ( state == 1) { state++ }
 }
}

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