grouping of objects and print their number


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Java grouping of objects and print their number

hello
have one file
N
H
H
K
L
K
K
H
K
N

so output shud be like
N = 2
H = 3
K = 4
L =1
# 2  
Code:
zsh 4.3.4% print 'N
H
H
K
L
K
K
H
K
N'|awk '{x[$1]++}END{for(i in x)printf "%s = %d\n",i,x[i]}' 
N = 2
H = 3
K = 4
L = 1

# 3  
Hi.

Using commands:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate counting with sort and uniq.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions of codes used in this script with local utility \"version\")"
version bash sort uniq

echo

FILE=${1-data1}

sort $FILE |
uniq -c

exit 0

Producing:
Code:
% ./s1

(Versions of codes used in this script with local utility "version")
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
sort (coreutils) 5.2.1
uniq (coreutils) 5.2.1

      3 H
      4 K
      1 L
      2 N

cheers, drl
# 4  
in Perl:
Code:
#!/usr/bin/perl
# count_letters.pl
use strict;
my %letters;
while (<>) {
    chomp;
    $letters{$_}++;
}
foreach my $letter (keys (%letters)) {
    print "$letter = $letters{$letter} \n";
}

Code:
[ysawant@in-gcs-nas1 temp]$ perl  count_letters.pl  letters_file 
H = 3 
N = 2 
K = 4 
L = 1 
[ysawant@in-gcs-nas1 temp]$

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #954
Difficulty: Easy
Boot sector viruses specifically target the boot sector and/or the Master Boot Record (MBR) of the infected host's hard disk drive, solid-state drive, or removable storage media (flash drives, floppy disks, etc.).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

4. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

5. Shell Programming and Scripting

Print line number

how i need help again i have file input.txt like this 48:20111008_20111122:31231|123| 78:20111008_20111122:435453|321112| where 48 and 78 is line number 20111008_20111122 is a file and 31231|123| i want get in file, i am using manual awk for get that file like this awk 'NR==48... (6 Replies)
Discussion started by: zvtral
6 Replies

6. Shell Programming and Scripting

HEX number grouping

Guys, I am looking for a small script which generates HEX sequence. Input to the script is starting hex number - Group ID and number of members in a group and total groups. e.g: we are generating 2 groups with 4 Members each starting with hex number 036A. I should get o/p in following format. ... (5 Replies)
Discussion started by: dynamax
5 Replies

7. UNIX for Dummies Questions & Answers

print duplicate number

input chr1 10 100 chr1 10 100 chr1 20 200 output chr1 10 100 2 chr1 20 200 1 (1 Reply)
Discussion started by: repinementer
1 Replies

8. Shell Programming and Scripting

Please help. How to print number with 'awk'?

Dear all, I have to transform a bunch of files into a new format. how to do it with bash script, especially with 'awk' command. the original data O,0,0.2839896442,-1.1628392411,-1.5976515479 Cr,0,-0.0502858628,-0.1725974837,-0.380627632 C,0,2.4506658279,-0.476964918,0.2299298249 ... (3 Replies)
Discussion started by: liuzhencc
3 Replies

9. Shell Programming and Scripting

How print number

Hi, I am using for loop as follow: for n in `ls` do echo "$n" done The code is running fine and aI am getting valid output as: jick zenny assi yogi But 1also want to print count in front of each output like this: 1 jick 2 zenny (4 Replies)
Discussion started by: bisla.yogender
4 Replies

10. Shell Programming and Scripting

to print number one less than actual number

suppose we have a file which contains 34 45 56 77 55 66 54 67 so output shud be like 33 44 55 76 54 65 53 66 (4 Replies)
Discussion started by: cdfd123
4 Replies

Featured Tech Videos