Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Items In Column 2 1

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
0
0
US
I have an input file called bsc_cells.txt that I need to count how many times
the items in column 2 appear.

The example input is below:

RXOTX-99-0 VA0449C ALL GSM1900 ALPHAC 45
RXOTX-99-1 VA0449C ALL GSM1900 ALPHAC 45
RXOTX-99-2 VA0449C ALL GSM1900 ALPHAC 45
RXOTX-99-3 VA0449C ALL GSM1900 ALPHAC 45
RXOTX-102-0 VA0465A ALL GSM1900 ALPHAA 45
RXOTX-102-1 VA0465A ALL GSM1900 ALPHAA 45
RXOTX-102-4 VA0465B ALL GSM1900 ALPHAB 45
RXOTX-102-5 VA0465B ALL GSM1900 ALPHAB 45
RXOTX-102-8 VA0465C ALL GSM1900 ALPHAC 45
RXOTX-102-9 VA0465C ALL GSM1900 ALPHAC 45
RXOTX-133-8 VA0475C ALL GSM1900 ALPHAC 45
RXOTX-133-9 VA0475C ALL GSM1900 ALPHAC 45
RXOTX-133-7 VA0475C ALL GSM1900 ALPHAC 45


The example output is below:

VA0449C 4
VA0465A 2
VA0465B 2
VA0465C 2
VA0475C 3
 

And what is the question? -- this is an extremely basic SQL statement to code.

Have you tried coding anything yet?

[neutral]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

Ooops, Unix forum -- sorry.

But still, you could maybe use awk to do it.

Have you coded anything?



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Code:
[b]#!/usr/bin/perl[/b]

open (IN, "< beaster.txt");
chomp (@data = <IN>);
close IN;

foreach (@data) {
  @array = split(/ +/);
  $col2{"$array[1]"}++;
}

while (($key, $value) = each %col2) {
  print "$key\t$value\n";
}


Kind Regards
Duncan
 
Another way:
awk '{++a[$2]}END{for(i in a)print i,a}' bsc_cells.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I just knew PHV would do that! ;-}

PHV - why does this not work in Perl using backticks?


Kind Regards
Duncan
 
why does this not work in Perl
Sorry, I don't know Perl.
 
This command...
Code:
cut -b19-25 bsc_cells.txt | sort | uniq -c
...gives this output...
Code:
   4 VA0449C
   2 VA0465A
   2 VA0465B
   2 VA0465C
   3 VA0475C
Hope this helps.
 
That is very nice feherke

Strangely enough i just keep overlooking the fact that Perl command-line solutions can be implemented!?

A * for you!

Please can you walk me through it

Code:
perl -ane

' [blue]start of [i]encapsulation[/i][/blue]

$a{$F[1]}++; [red]what is $F[1] ?[/red]

END {

[b]foreach (keys %a) {
  print"$_ $a{$_}\n"
}[/b]

} [red]# end of 'END' block[/red]

' [blue]end of [i]encapsulation[/i][/blue]

input_file.txt


Kind Regards
Duncan
 
Hi

A little trick : [tt]Perl[/tt] man pages are enormous. The [tt]perl --help[/tt] is more simple.

-a autosplit mode with -n or -p (splits $_ into @F)
-n assume 'while (<>) { ... }' loop around program

With this two switches explained I hope the code is clear.

Feherke.
 
Wicked!

Thanks feherke

* for that interesting solution


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top