Count and print the most repeating string in each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count and print the most repeating string in each line
# 8  
Old 03-17-2015
Hi, in builtin bash ( bash 4 or higher) just for fun:
Code:
#!/bin/bash
unset XX
declare -A XX
while read A B
do
        [[ ${XX[$A]} =~ ${B}_[0-9]* ]] && { C=${B}_$((${BASH_REMATCH[@]//*_/}+1)) ; XX[$A]=${XX[$A]/${BASH_REMATCH[@]}/${C}} ; } || XX[$A]+="${B}_1 "
done <input1.txt

for i in ${!XX[@]}
do
        b=""
        a=0
        for j in ${XX[$i]}
        do
                [ ${j//*_/} -ge $a ] && { a=${j//*_/} ; [ ${j//*_/} -eq $a ] && b="$b ${j//_*/} $a" || b="${j//_*/} $a" ;}
        done
        echo $i $b
done

Regards.

Last edited by disedorgue; 03-17-2015 at 04:08 PM.. Reason: To notify bash version
# 9  
Old 03-17-2015
Quote:
Originally Posted by disedorgue
Hi, in builtin bash just for fun:
Code:
#!/bin/bash
unset XX
declare -A XX
while read A B
do
        [[ ${XX[$A]} =~ ${B}_[0-9]* ]] && { C=${B}_$((${BASH_REMATCH[@]//*_/}+1)) ; XX[$A]=${XX[$A]/${BASH_REMATCH[@]}/${C}} ; } || XX[$A]+="${B}_1 "
done <input1.txt

for i in ${!XX[@]}
do
        b=""
        a=0
        for j in ${XX[$i]}
        do
                [ ${j//*_/} -ge $a ] && { a=${j//*_/} ; [ ${j//*_/} -eq $a ] && b="$b ${j//_*/} $a" || b="${j//_*/} $a" ;}
        done
        echo $i $b
done

Regards.
When I try this with the following version of bash:
Code:
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)
Copyright (C) 2007 Free Software Foundation, Inc.

I get the output:
Code:
disedorgue: line 3: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
0 BP1 3 BP3 4

If I change line 3 to:
Code:
declare -a XX

or comment out that line, I get the output:
Code:
0 BP1 3 BP3 4

Which version of bash is required to make this work?
# 10  
Old 03-17-2015
Bash 4 or higher:
Code:
$ bash -c 'declare -A XX'
bash: line 0: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
$ bash4 -c 'declare -A XX'
$

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count occurences of the word without it repeating

Hi, I would like to count the number of ALA occurences without having them to be repeated. In the script I have written now it has 40 repetitions of ALA but it has to be 8. ALA is chosen as one of the 20 values it can have when the script asks for the input of AAA, which for this example is chosen... (7 Replies)
Discussion started by: Aurimas
7 Replies

2. Shell Programming and Scripting

How to print line starting with certain string together with its following line?

Dear all, How can I print line starting with certain string together with its following line. Example is as follows: Input file: @M01596:22:000000000-A7YH7:1:1101:16615:1070 2:N:0:1... (2 Replies)
Discussion started by: huiyee1
2 Replies

3. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

4. UNIX for Dummies Questions & Answers

How to count a string in a line and report it?

Hi, I have a text file full of such line (this is only 1 line, tab delimited): 1 108 . C T 553.90 . ... (19 Replies)
Discussion started by: a_bahreini
19 Replies

5. Shell Programming and Scripting

Print String Every Specific Line

Dear All, I have input file like this, 001 059 079 996 758 079 069 059 079 ... ... Desired output: AA 001 BB 059 (4 Replies)
Discussion started by: attila
4 Replies

6. Shell Programming and Scripting

Compare last 90 logs and print repeating lines with >20

*log files are in date order sample logs... ciscoresets_20120314 ciscoresets_20120313 ciscoresets_20120312 ciscoresets_20120311 ciscoresets_20120310 cat ciscoresets_20120314 SYDGRE04,10,9 SYDGRE04,10,10 SYDGRE04,10,11 SYDGRE04,10,12 SYDGRE04,10,13 SYDGRE04,10,14 SYDGRE04,10,15... (2 Replies)
Discussion started by: slashbash
2 Replies

7. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

8. Shell Programming and Scripting

Count and print all repeating words in a line

Gurus, I have a file containing lines like this : Now, number of words in each line varies. My need is, if a word repeats in a line get it printed. Also total number of repeats. So, the output would be : Any help would be highly appreciated. Thanks & Regards (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

9. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

10. Shell Programming and Scripting

Grep a string and print a string from the line below it

I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate: Name 1: ABC Age: 3 Sex: Male Name 2: DEF Age: 4 Sex: Male Output: 3 Male I know how to get "3". My biggest problem is to... (4 Replies)
Discussion started by: kingpeejay
4 Replies
Login or Register to Ask a Question