LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 01-13-2010, 01:58 AM   #1
cs24
LQ Newbie
 
Registered: Nov 2009
Location: Chinese
Distribution: Centos 5.2
Posts: 23

Rep: Reputation: 1
Column statistic by awk ?


hi,all
here is the txt file
Code:
aaa 1
aaa 2
aaa 3
bbb 4
bbb 5
ccc 6
ccc 7
ccc 8
I want to use a script to statistic the results like that
aaa 3
bbb 2
ccc 3

I want to know the the same string in the first column and how many different in the second column ?
how awk to write ?
 
Old 01-13-2010, 02:58 AM   #2
Ni2
LQ Newbie
 
Registered: Nov 2004
Posts: 6

Rep: Reputation: 0
try
Code:
$ cat txt
aaa 1
aaa 2
aaa 3
bbb 4
bbb 5
ccc 6
ccc 7
ccc 8

$ cut -d' ' -f1 txt | uniq -c | awk '{print $2,$1}'
aaa 3
bbb 2
ccc 3
 
Old 01-13-2010, 05:03 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk '{a[$1]++}END{for(i in a)print i,a[i]}' file
 
Old 01-14-2010, 02:50 AM   #4
cs24
LQ Newbie
 
Registered: Nov 2009
Location: Chinese
Distribution: Centos 5.2
Posts: 23

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by ghostdog74 View Post
Code:
awk '{a[$1]++}END{for(i in a)print i,a[i]}' file
it's work.thanks.
and now there are some new situation .
[code]
aaa 1 d
aaa 1 e
aaa 2 e
bbb 3 f
bbb 3 g
ccc 4 h
ccc 5 i
ccc 6 h
[/cdoe]
the second column also have the same word.and after the second column ,it's anomaly.now I run two command to statistics .
first I run
Code:
awk '{print $1,$2}' test.txt | sort -u > test2.txt
and then run
Code:
awk '{a[$1]++}END{for(i in a)print i,a[i]}' test2.txt
can I just run one command to finish this job ? how to do that ?
 
Old 01-14-2010, 03:15 AM   #5
Ni2
LQ Newbie
 
Registered: Nov 2004
Posts: 6

Rep: Reputation: 0
Try running

Quote:
Originally Posted by ghostdog74 View Post
Code:
awk '{a[$1]++}END{for(i in a)print i,a[i]}' file
on test.txt (The file with 3 columns).

It should give you the count based on the first field. I don't think you need to do this.

Code:
awk '{print $1,$2}' test.txt | sort -u > test2.txt
 
Old 01-14-2010, 04:40 AM   #6
cs24
LQ Newbie
 
Registered: Nov 2009
Location: Chinese
Distribution: Centos 5.2
Posts: 23

Original Poster
Rep: Reputation: 1
really?i will try!
 
Old 01-15-2010, 02:32 AM   #7
cs24
LQ Newbie
 
Registered: Nov 2009
Location: Chinese
Distribution: Centos 5.2
Posts: 23

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Ni2 View Post
Try running



on test.txt (The file with 3 columns).

It should give you the count based on the first field. I don't think you need to do this.

Code:
awk '{print $1,$2}' test.txt | sort -u > test2.txt
no,it doesn't work.in the second column ,the same word will be count again .so the results not right .
 
Old 01-15-2010, 05:41 AM   #8
Ni2
LQ Newbie
 
Registered: Nov 2004
Posts: 6

Rep: Reputation: 0
Quote:
Originally Posted by cs24 View Post
no,it doesn't work.in the second column ,the same word will be count again .so the results not right .
Well this is what I get. What do you get?

Code:
$cat file1
aaa 1 d
aaa 1 e
aaa 2 e
bbb 3 f
bbb 3 g
ccc 4 h
ccc 5 i
ccc 6 h
$awk '{a[$1]++}END{for(i in a)print i,a[i]}' file1
aaa 3
ccc 3
bbb 2
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Change column value with AWK RyudoBlaze Programming 2 04-25-2009 03:05 AM
sed/awk group on first column Eddie Adams Linux - General 4 04-09-2009 10:23 AM
awk comparing a column value with a stored variables value bugg_deccan Programming 4 12-05-2008 07:08 AM
Multiplying data column in awk - can rounding be suppressed? johnpaulodonnell Linux - Newbie 2 02-28-2007 07:56 AM
awk column printing schneidz Programming 7 09-29-2005 06:14 AM


All times are GMT -5. The time now is 12:11 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration