Sponsored Content
Top Forums Shell Programming and Scripting Count the Consecutive Occurance of "X" in awk Post 302256897 by nica on Monday 10th of November 2008 10:13:44 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy