LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2016, 12:06 AM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 93

Rep: Reputation: 0
Question Counting repeated entries


Guys, I am lost here..

I have a file with following format

Quote:
dn: 1st
cn: asdsd
cn: adasd
sn: ad

dn: 2nd
cn: 123
cn: 12312
cn: 1
sn: aansd
sn: aa
Want to get such an output

Quote:
dn: 1st
cn: 2 (total)
sn: 1 (total)

dn: 2nd
cn: 3 (total)
sn: 2 (total)
The format doesn't to be as above.. but basically print the dn line and print how many times cn is repeating for each dn.. i can feed in the attribute name manually so it does not have to magically find all attributes and count them.. So can pipe the file into some awk or something with mentioning dn , cn, sn and it will print the above

Thanks
 
Old 06-16-2016, 01:50 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
have you considered using uniq?
Code:
man uniq
(...)
 -c, --count
              prefix lines by the number of occurrences
 
Old 06-16-2016, 01:52 AM   #3
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ondoho View Post
have you considered using uniq?
Code:
man uniq
(...)
 -c, --count
              prefix lines by the number of occurrences
Yes.. But how do u use it to get the above output? its a huge file with multiple dn prints with counts under it
 
Old 06-16-2016, 02:23 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
quiet day at work

Code:
#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my $dn;
my %H ;

while (<>) {

    # print;   # debug
    chomp;
    /^dn:/ and $dn = $_;
    next unless defined $dn;
    $H{$dn}->{count}++ if /^cn:/;

}

print Dumper(\%H);
Code:
[billy@donald:0]$ ./khandu.pl 1

dn: 1st
cn: asdsd
cn: adasd
sn: ad

dn: 2nd
cn: 123
cn: 12312
cn: 1
sn: aansd
sn: aa 
$VAR1 = {
          'dn: 2nd' => {
                         'count' => 3
                       },
          'dn: 1st' => {
                         'count' => 2
                       }
        };
 
1 members found this post helpful.
Old 06-16-2016, 02:29 AM   #5
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
awesome.. how do i do it for multiple counts

like

Quote:
dn: 2nd
cn: 123
cn: 12312
cn: 1
sn: aansd
sn: aa

dn: 2nd
cn: count 3
sn: count 2
 
Old 06-16-2016, 02:40 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about you try and do some of the work yourself. You have been shown a possible solution so now it is up to you to try and augment it as you need.
 
1 members found this post helpful.
Old 06-16-2016, 02:43 AM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by khandu View Post
awesome.. how do i do it for multiple counts
Use *logic*.

Pseudocode:

Code:
if line starts with dn
   if line starts with cn or sn
        start counting
   if line starts with dn
        print count
        reset counter
Best regards,
HMW

Last edited by HMW; 06-16-2016 at 02:44 AM.
 
Old 06-16-2016, 02:47 AM   #8
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
lol..

I tried

Quote:
#!/usr/bin/perl -w

use strict;
use Data:umper;

my $dn;
my %H ;
my %J ;


while (<>) {

# print; # debug
chomp;
/^dn:/ and $dn = $_;
next unless defined $dn;
$H{$dn}->{count}++ if /^cn:/;
$J{$dn}->{count}++ if /^sn:/;
}

print Dumper(\%H);
print Dumper(\%J);
gives me what i need.. need to make it a bit more prettier and with actual words of cn and sn etc in it..
 
Old 06-16-2016, 03:01 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
You may try another language too, I do not know which one do you prefer (awk/python/perl/whatever).
Here is another approach (pseudocode too)
Code:
if line starts with dn:
   new record
   ix += 1  (index, counter of dn's)
else
   split line to name/value pairs
   store name/value in record[ix] (this looks like hash or associative array)

finally count the number of elements in the hash (or count number of occurrences of something or ...)
By the way what will/should happen if you have two identical lines next to each other?
 
Old 06-16-2016, 03:03 AM   #10
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Thanks guys.. currently its giving me basic of what i need in a dirty way which is ok..

have modified the print of $VAR1 via qw..

will get back if i hit a hiccup..
 
Old 06-16-2016, 03:08 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
haha! I enjoy a bit of nifty Perl! I am trying to avoid doing a conformance test today anyway


Code:
#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my $dn;
my %H ;
my $thing;

while (<>) {

    print;
    chomp;
    next unless /./;

    if (m/^dn:/) {
        $dn = $_;
        next;
    }
    next unless defined $dn;
    next unless ($thing) = /^(\w+):/;
    $H{$dn}->{$thing}++ ;

}

print Dumper(\%H);
Code:
$VAR1 = {
          'dn: 2nd' => {
                         'cn' => 3,
                         'sn' => 2
                       },
          'dn: 1st' => {
                         'cn' => 2,
                         'sn' => 1
                       }
        };
Now I better get on with my work
 
1 members found this post helpful.
Old 06-16-2016, 04:04 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk '/dn/{for(i in a)print i,a[i];delete a;print}/[cs]n/{a[$1]++}END{for(i in a)print i,a[i]}' file
Might need to play with sort orders but you get the idea
 
Old 06-16-2016, 04:11 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by grail View Post
... but you get the idea
Not 90+% of the population I suspect ....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Explain ark.intel.com CPU counting and linux counting? postcd Linux - Hardware 9 06-27-2014 01:24 PM
How to schedule for a repeated task? hadimotamedi Linux - Newbie 7 05-04-2010 02:58 AM
2.6.24 repeated keystrokes bug? Arnaud_B Debian 3 03-04-2008 11:51 AM
how to sort output at latest entries without disturbing the previous entries record nabmufti Programming 4 02-11-2008 11:36 PM
Repeated Error with C compiler morelikebovine Linux - Software 4 04-08-2006 04:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration