sorting/arrangement problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sorting/arrangement problem
# 1  
Old 05-23-2007
sorting/arrangement problem

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


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

the output I expect is:

aSmiliec: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  
Old 05-24-2007
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-24-2007 at 12:40 AM..
# 3  
Old 05-24-2007
You are a man of few words Smilie ! 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  
Old 05-24-2007
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  
Old 05-24-2007
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  
Old 05-24-2007
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  
Old 05-25-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem with sorting in shell script

Hi, I have the following list: 42A 42AA 42B 42BB 42AAA 42BBB 49A 49AA 49B 49AAA 49BB I need it to be sorted as following: 42A 42B (2 Replies)
Discussion started by: sairamtejaswi
2 Replies

2. UNIX for Advanced & Expert Users

Sorting problem

I have data like this in file Now I have to sort using second column(usage) and i should get first column against it post sorting please provide input (2 Replies)
Discussion started by: mirwasim
2 Replies

3. Shell Programming and Scripting

Help with data re-arrangement problem facing

Input file: <symbol>Q9Y8G1</symbol> <name>Q9Y8G1_EMENI</name> <symbol>Q6V953</symbol> <symbol>Q5B8K1</symbol> <name>Q6V953_EMENI</name> <symbol>G1A416</symbol> <name>G1A416_9FUNG</name> <symbol>D4N894</symbol> <name>D4N894_PLEER</name> <symbol>B0FFU4</symbol>... (13 Replies)
Discussion started by: cpp_beginner
13 Replies

4. Shell Programming and Scripting

Help to data re-arrangement problem

Input file <data id>="1">\ </data>\ <data id>="2">\ </data>\ <code>="1" target="2">\ </code>\ <data id>="1">\ </data>\ <data id>="2">\ </data>\ <code>="1" target="2">\ </code>\ <data id>="1">\ </data>\ <data id>="2">\ </data>\ <code>="1" target="2">\ </code>\ (2 Replies)
Discussion started by: cpp_beginner
2 Replies

5. UNIX for Dummies Questions & Answers

Sorting problem

1) I need to reverse sort the text from lit.csv (bellow) by the second column, then save the last five lines in a text file text.txt but I'm doing something wrong so any help would be much appreciated. I've been trying to use this: sort --field-separator=; -b -k2g,2 lit.csv -o text.txt 2)... (4 Replies)
Discussion started by: Kova
4 Replies

6. UNIX for Dummies Questions & Answers

Sorting file with columns (problem)

Hi all! I am new to Unix programming so bare with me please :). I have saved the output of my results in a file called testfile which contains 3 columns a 15 rows. e.g. 175 754 abvd 948 454 fewf 43 754 fewc 6 734 feww xxx xxx xxxx I want to sort the contents of this file... (10 Replies)
Discussion started by: daelas
10 Replies

7. Shell Programming and Scripting

Sorting problem (SunOS 5.9 / KSH).

Hi, Can someone tell me why the "LargeFile" is coming first before the smaller files. Is there any way to list the files based on size column. ls -g| sort -k 4 -rw-r--r-- 1 user 6117910528 Apr 28 15:04 LargeFile -rw-r--r-- 1 user 6136832 May 30 07:23 my_20080530.tar -rw-r--r-- ... (2 Replies)
Discussion started by: kesari
2 Replies

8. UNIX for Dummies Questions & Answers

Sorting problem

Hai , In unix we are going to sort it out all the directories and files based on size of the file. For that we have to use this command ls -al | sort +4nr. If we are giving this command means it will show all the records in a descending order....when I am checking one file bytes its... (4 Replies)
Discussion started by: cool4naren
4 Replies

9. HP-UX

Sorting problem....

hey mate. ive got a sorting problem that i would like to share... i made a script that sorts the bdf command and redirected the output to a file. the output of the file is: 691416 34% / 851552 7% /stand 7203048 31% /var 23637584 26% /var/adm/crash 2968864 ... (4 Replies)
Discussion started by: jarod004
4 Replies

10. Shell Programming and Scripting

Problem in sorting the file

file format is word filename no.of occurances ------------------------------ wish f3.txt 2 wish f2.txt 1 cold f1.txt 5 cold f2.txt 3 cold f1.txt 3 cold e.txt 3 gold f1.txt 7 gold f3.txt 3 rush e.txt 2 itz abt building a search index for every word in the files given as input the... (1 Reply)
Discussion started by: vishnu_vaka
1 Replies
Login or Register to Ask a Question