Count consecutive characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count consecutive characters
# 8  
Old 02-06-2018
Another one:
Code:
echo aaaabbcaa | sed 's/\(.\)\1*/& /g' | awk '{for(i=1; i<=NF; i++) $i=substr($i,1,1) length($i)}1' OFS=

# 9  
Old 02-06-2018
Quote:
Originally Posted by rdrtx1
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"

Nice idea.

However I would avoid using match() and sub() as these will interpret regex characters. [ in the input causes fatal Unmatched error and ? or . characters cause incorrect output.

Code:
$ ip="aaa[b]"
$ op=$(echo "$ip" | awk '{while (/./) {c=substr($0, 1, 1); match($0, c "*", a); printf c RLENGTH; sub(a[0], "")}}')
awk: cmd. line:1: (FILENAME=- FNR=1) fatal: Unmatched [, [^, [:, [., or [=: /[*/

$ ip="aaa???bbb"
$ op=$(echo "$ip" | awk '{while (/./) {c=substr($0, 1, 1); match($0, c "*", a); printf c RLENGTH; sub(a[0], "")}}')
$ echo $op
a3?3?2?1b3

ip="aaa.bbb"
$ op=$(echo "$ip" | awk '{while (/./) {c=substr($0, 1, 1); match($0, c "*", a); printf c RLENGTH; sub(a[0], "")}}')
$ echo "$op"
a3.4


Last edited by Chubler_XL; 02-06-2018 at 06:03 PM..
# 10  
Old 02-11-2018
Code:
ip=aaaabbcaa
op=
for s in `echo $ip |sed -r 's/((\w)\2*)/\1 /g'; do  op=$op${s:0:1}${#s}; done

# op= a4b2c1a2


Last edited by abdulbadii; 02-13-2018 at 05:43 PM..
This User Gave Thanks to abdulbadii For This Post:
# 11  
Old 02-13-2018
Nice use of backreferences!
Here is a Posix variant:
Code:
#!/bin/sh
 ip=aaaabbcaa
 op=
 for s in `echo "$ip" | sed 's/\(\(.\)\2*\)/\1 /g'`
 do
  del=${s#?}
  op=$op${s%$del}${#s}
 done
 echo "$op"

And a variant of Chubler's post#6:
Code:
#!/bin/bash
ip=aaaabbcaa
len=${#ip}
lchar=${ip:0:1}
for((i=1; i<=$len; i++))
do
  ((found++))
  char=${ip:i:1}
  if [[ $char != $lchar ]]
  then
    op=$op$lchar$found
    found=0
    lchar=$char
  fi
done
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