Count consecutive characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count consecutive characters
# 1  
Old 02-05-2018
Count consecutive characters

Need to count consecutive characters in a string and give the output as below
i/p=aaaabbcaa
o/p=a4b2c1a2

Last edited by rbatte1; 02-05-2018 at 07:52 AM..
# 2  
Old 02-05-2018
Any attempts / ideas / thoughts from your side?
# 3  
Old 02-05-2018
Quote:
Originally Posted by RudiC
Any attempts / ideas / thoughts from your side?
Tried something like this, but I guess this will work only for first identical characters (a) and will not work for b.

Code:
i/p=aaaabbcaa
length=${#i/p} 

for (i=0;i<$length;i++)
do
tmp=""
character=${ip:"$i"}
	if [[ $character != $tmp ]]
	then
	o/p=$character
	tmp=$character
	else
	o/p=$character$i
	fi
done

# 4  
Old 02-05-2018
Hmmm - there's quite some syntax errors in your script (assuming your (unmentioned) shell is bourne type, e.g. bash, or ksh), obviously logic error(s) as well. Would you mind to use an awk solution?
Code:
awk  '
        {LAST = $1
         CNT = 0
         for (i=1; i<=NF; i++)  {if ($i == LAST) CNT++
                                 else           {printf "%s%d", LAST, CNT
                                                 CNT = 1
                                                }
                                 LAST = $i
                                }
         printf "%s%d\n", LAST, CNT
        }
' FS="" file
a4b2c1a2

given your awk version allows for a zero length field separator yielding every single char in the input line as a field of its own .
This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-05-2018
Code:
echo aaaabbcaa | fold -w 1 | uniq -c | awk '{l=l$2$1} END {print l}'

These 3 Users Gave Thanks to rdrtx1 For This Post:
# 6  
Old 02-05-2018
Here is a bash solution.

Note slash (/) is not allowed as a part of variable names, so I renamed i/p and o/p to ip and op respectively

Code:
ip=aaaabbcaa

for((i=0; i<${#ip}; i++))
do
    ((found++))
    character=${ip:i:1}
    nextchar=${ip:i+1:1}
    if [[ $character != $nextchar ]]
    then
        op=$op$character$found
        found=0
    fi
done
echo $op

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 02-06-2018
Code:
ip="aaaabbcaa"
op=$(echo "$ip" | awk '{while (/./) {c=substr($0, 1, 1); match($0, c "*", a); printf c RLENGTH; sub(a[0], "")}}')
echo "$op"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Consecutive count and reset in awk

I have the following file: A1 4.5807 6.4202 B1 2.5704 11.4414 C1 5.5607 5.28872 D1 3.5807 8.2132 E1 3.2206 9.13153 F1 3.0907 9.51532 G1 3.2707 8.99165 H1 2.4607 11.9515 A2 2.5505 11.5307 B2 2.3106 12.7279 C2 3.8507 7.63731 D2 2.6208 11.2214 E2 2.7609 10.652 F2 2.0604 14.2734 G2... (2 Replies)
Discussion started by: Xterra
2 Replies

2. Shell Programming and Scripting

Bash only count consecutive days

I was wondering how I would go around to do this. This is an example of my output Sun Aug 21 2016 03:00:00, BLAH Mon Aug 22 2016 03:54:00, BLAH Tue Aug 23 2016 04:22:11, BLAH Thu Aug 25 2016 05:00:00, BLAH Now what I would like to do is only count CONSECUTIVE days so in the... (1 Reply)
Discussion started by: rodriguesm
1 Replies

3. Shell Programming and Scripting

[Solved] Count characters of variable from right

My variable is something like: f="/Volumes/VERVE/MOOTON_CALL/01_shots/XX/xx0195/Projects/program/rs0195_v400001.aep" I use ${f:63:6} to call "rs0195" as characters counted from the left, but it'd be so much easier to count from the right. If ${f:95:10} counts from the left, what would... (2 Replies)
Discussion started by: scribling
2 Replies

4. Shell Programming and Scripting

Count characters after a line

Hi All! I would like to solve a problem but I have no clue of how do it!I will be grateful if someone could help me! I have a file like this: > genes | transcript ...sequence.... >ENSMUSG00000006638|ENSMUST00000006814 GGGAAATGGAATACCCCTACACAACCAAGATGCTGAGTTCCTCCCTGAGCCCGCAAAACG... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

5. UNIX for Dummies Questions & Answers

count different characters from one column

Hi everyone, and thanks after all I'm a biologist and i have to extract information from one text. The text is something like this 1023 A A 56 0 cc...,,,,..gg..Cc.c,,c..CC..,, 1024 T T 86 0 ..,,,..aaAA..,,aAA,,a,,A,,a 1025 G G 125 0 ... (5 Replies)
Discussion started by: beajorrin
5 Replies

6. Shell Programming and Scripting

Count number of characters in particular column

Hi i have data like abchd 124 ldskc aattggcc each separated by tab space i want to count number of characters in 4th column and print it in new column with tabspace for every line can anyone help me how to do it. Thanks. (3 Replies)
Discussion started by: bhargavpbk88
3 Replies

7. UNIX for Dummies Questions & Answers

deletion of duplicate characters and count

to delete the duplicate characters in a file I used this code cat file.txt|tr -s "" tell the other ways using sed command to count of duplicate characters thanks:) (0 Replies)
Discussion started by: tsurendra
0 Replies

8. UNIX for Dummies Questions & Answers

Count the characters in a string

Hi all, I like to know how to get the count of each character in a given word. Using the commands i can easily get the output. How do it without using the commands ( in shell programming or any programming) if you give outline of the program ( pseudo code ) i used the following commands ... (3 Replies)
Discussion started by: itkamaraj
3 Replies

9. Shell Programming and Scripting

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 (16 Replies)
Discussion started by: nica
16 Replies

10. Shell Programming and Scripting

how to count characters by line of file ?

Hello, Member or professional need help how to count characters by line of file Example of the file is here cdr20080817164322811681txt cdr20080817164322811txt cdr20080817164322811683txt cdr20080817164322811684txt I want to count the characters by line of file . The output that I... (4 Replies)
Discussion started by: ooilinlove
4 Replies
Login or Register to Ask a Question