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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sorting problem cool4naren UNIX for Dummies Questions & Answers 4 07-31-2007 08:28 AM
Sorting problem.... jarod004 HP-UX 4 06-25-2007 12:42 AM
Sorting/Filed Defining/Output problem TiredOrangeCat Shell Programming and Scripting 1 02-12-2007 12:41 AM
Data arrangement bobo UNIX for Dummies Questions & Answers 6 09-21-2006 10:36 AM
Problem in sorting the file vishnu_vaka Shell Programming and Scripting 1 12-04-2005 11:47 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2007
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
sorting/arrangement problem

Hi,
I have the following 'sorting' problem.
Given the input file:


ac:12:x:k
s:m:d:8:z:m
ac:1:x:k
p:q:r:23:y:m
ac:3:x:k
p:q:r:1:y:m

the output I expect is:

ac:1:x:k
p:q:r:1:y:m
s:m:d:8:z:m


What happened here is I grouped together lines having the same values for columns 1,2,3,5,6 and selected only the line with the smallest value in the 4th column in every group.

I am currently managing this with a small PERL script. Is there a way to do this using shell utilities alone?

Regards...
  #2 (permalink)  
Old 05-23-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try...
Code:
awk 'BEGIN{FS=":"}{c=$4;b=$0;$4="";k=$0}!v[k]||v[k]>c{v[k]=c;a[k]=b}END{for(i in a)print a[i]}' file1

Last edited by Ygor; 05-23-2007 at 11:40 PM..
  #3 (permalink)  
Old 05-24-2007
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
You are a man of few words ! Amazing!
My unix machine is down, and it will be a while before I can try this out. But now that I have seen it, I can tweak it, if I do run into any errors.

Thanks!
  #4 (permalink)  
Old 05-24-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
Another way:
Code:
sort -t':' -k1,3 -k5,6 -k4,4n s1.txt | \
awk  -F':' '{ line=$0; $4=""; if ($0 != key) print line; key=$0 }' inputfile
Jean-Pierre.
  #5 (permalink)  
Old 05-24-2007
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
error

there is a slight problem i am encountering.......

reassigning $4, does not reassign $0. So both of the above methods are failing....any cures?
  #6 (permalink)  
Old 05-24-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
A little correction to my solution which works fine on my AIX box (do you try with nawk or gawk if they are available on your system ?) :
Code:
sort -t':' -k1,3 -k5,6 -k4,4n inputfile  | \
awk  -F':' '{ line=$0; $4=""; if ($0 != key) print line; key=$0 }'
The following solution doesn't modify fields, so it must work with all versions of awk :
Code:
sort -t':' -k1,3 -k5,6 -k4,4n inputfile  | \
awk  -F ':' '{ key=$1 ":" $2 ":" $3 ":" $5 ":" $6; if (key != prv_key) print; prv_key=key }'
Jean-Pierre.
  #7 (permalink)  
Old 05-25-2007
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
Tweaked for my system

Hi,

I made small change to the code posted by Ygor...and that will apply to solution by Aigles too (remember, my problem is reassigning $4 doesnt modify $0). Heres the changed code:

awk 'BEGIN{FS=":"}{str=""; for(j=1;j<=NF;++j) str=sprintf("%s%s",str,$j)} (v[str]=="")||(v[str]>$4){v[str]=$4;a[str]=$0}END{for(i in a)print a[i]}' file1


Here are the tweaks:
(1) Now I have a new variable "str" - this doesnt have the 4th column values
(2) In the condition part, now I check for (v[str]=="") instead of (!v[str]) --- the latter throws an error on my system.

Thanks to everyone for the effort....

Regards, Abhishek
Sponsored Links
Closed Thread

Bookmarks

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 04:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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