The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Append the data to first column ravi_rn Shell Programming and Scripting 2 06-02-2009 01:08 PM
alter data in column tintin72 UNIX for Dummies Questions & Answers 2 04-02-2009 07:40 AM
remove a column of data jdhahbi Shell Programming and Scripting 1 01-22-2009 09:29 PM
Need to get the first column of data bobk544 Shell Programming and Scripting 3 08-04-2007 04:35 PM
Column data reading FarhanNaseer Shell Programming and Scripting 1 09-19-2005 12:02 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-02-2009
anjas anjas is offline
Registered User
  
 

Join Date: Mar 2009
Location: Bali, Indonesia
Posts: 17
counting the data in column

Hi power user,

I have data as follows

aa bb
bb aa
aa bb
bb aa
bb cc
cc bb
bb cc
zz ll
ll zz
zz ll

and I want the output to be like this:

aa bb 4
cc bb 3
zz ll 3

So, althrough the data in the column are arranged in a different order, they will be considered the same data. Is there any awk script which could do that? Tx
  #2 (permalink)  
Old 07-02-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
What have you tried so far?
  #3 (permalink)  
Old 07-03-2009
anjas anjas is offline
Registered User
  
 

Join Date: Mar 2009
Location: Bali, Indonesia
Posts: 17
sort file |uniq -c doesn't seems to give that output, since it considered aa bb and bb aa as a different kind of data. I need a script to consider them as the same kind of data.
  #4 (permalink)  
Old 07-03-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
ok, how far have you gotten with the script implementation?

Last edited by vgersh99; 07-03-2009 at 11:23 AM..
  #5 (permalink)  
Old 07-03-2009
anjas anjas is offline
Registered User
  
 

Join Date: Mar 2009
Location: Bali, Indonesia
Posts: 17
So, this what I do. The content of the file is as follow:

try.txt

aa bb
bb aa
aa bb
bb aa
bb cc
cc bb
bb cc
zz ll
ll zz
zz ll

I did the usual sort command:

sort try.txt |uniq -c > try.sort

The content of try.sort is:
2 aa bb
2 bb aa
2 bb cc
1 cc bb
1 ll zz
2 zz ll

What I need is the output to be like this
aa bb 4
cc bb 3
zz ll 3
I think, the script should included, how to switch the data in the same order. For example:

zz ll
ll zz
zz ll

should be changed to
zz ii
zz ii
zz ii

Is there any clue for it? Tx
  #6 (permalink)  
Old 07-03-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
nawk -f an.awk myFile

an.awk:
Code:
{idx1=$1 OFS $2; idx2=$2 OFS $1}
{a[(idx2 in a)?idx2:idx1]++}
END { for (i in a) print i, a[i] }
  #7 (permalink)  
Old 07-03-2009
anjas anjas is offline
Registered User
  
 

Join Date: Mar 2009
Location: Bali, Indonesia
Posts: 17
Tx..It works as expected..
Sponsored Links
Reply

Bookmarks

Tags
columns, count words

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0