UNIX sort


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers UNIX sort
# 1  
Old 02-01-2015
UNIX sort

Hi I have below pattern

Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: pens   4  B:Bags 4   total count = 8
A: pens   4  B:Bags 4
A: cells    6
A: jobs    6

Output I need :

Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: pens   4  B:Bags 4   total count = 8
A: cells    6
A: jobs    6

2nd line and 3rd lines of 6 columns are same . So I need to remove 3rd line.(which all 6 columns are same)
Any help ??
Thanks for advance

Last edited by Don Cragun; 02-01-2015 at 09:37 PM.. Reason: Add CODE tags.
# 2  
Old 02-01-2015
Quote:
Originally Posted by pkkanduk
Hi I have below pattern

Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: pens   4  B:Bags 4   total count = 8
A: pens   4  B:Bags 4
A: cells    6
A: jobs    6

Output I need :

Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: pens   4  B:Bags 4   total count = 8
A: cells    6
A: jobs    6

2nd line and 3rd lines of 6 columns are same . So I need to remove 3rd line.(which all 6 columns are same)
Any help ??
Thanks for advance
Your statements are inconsistent.

The title of this thread is UNIX sort which lead me to believe that you wanted to use the sort command's -u option to discard all but one line for each set of lines with duplicate keys.

But sort uses a single character as a field separator (except for the default case which uses sequences of one or more spaces and tabs as field separators). So, with your input, the first six columns (or fields) in input line 2 are:
Code:
A: pens   4  B:Bags 4   total

and there are only five columns in input line 3. If you meant that you wanted to sort on five fields (instead of six), the output order would be very different from what you said you want and the standards don't specify whether you would get line 2 or line 3 as the output from those two lines. Assuming that your text to be processed is in a file named file, the command:
Code:
sort -u -k1,5 file

would produce the output:
Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: cells    6
A: jobs    6
A: pens   4  B:Bags 4   total count = 8

or the output:
Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: cells    6
A: jobs    6
A: pens   4  B:Bags 4

If this isn't the output you were trying to get, please clearly explain what you are trying to do.
# 3  
Old 02-01-2015
Sorry for the confusion.
I am trying to use unix sort on below file which contains
Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: pens   4  B:Bags  4
A: pens   4  B:Bags  4   total_count = 8
A: pens   4  B:Bags  4
A: pens   4  B:Bags  4   total_count = 8
A: cells  6
A: jobs   6

Output which I am expecting is :
Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: pens   4  B:Bags  4   total_count = 8
A: cells  6
A: jobs   6

I tried
Code:
 sort -u -k1,5

output is
Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: cells  6
A: jobs   6
A: pens   4  B:Bags  4

Output which I am expecting is
Code:
A: Apple  2  B:Bolls 4   total_count = 6
A: pens   4  B:Bags  4   total_count = 8
A: cells  6
A: jobs   6

# 4  
Old 02-02-2015
I don't see, how sort alone could solve this. Because it would need to decide to discard:
Code:
A: pens   4  B:Bags  4

while keeping
Code:
A: pens   4  B:Bags  4   total_count = 8


--
Could you try if this is closer to what you need:
Code:
awk 'NF!=5' infile | uniq

if the file is already sorted on field number 3, or otherwise..
Code:
awk 'NF!=5' infile | sort -u -k3,3n -k1,2 -k4

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-02-2015
It's working for above code and trying to implement same on below pattern
Code:
A: Apple  2  B:Bolls  4   total_count = 6
A: pens   4  
A: pens   4  
A: pens   4  B:Bags  4   total_count = 8
A: pens   4  
A: pens   4  B:Bags  4   total_count = 8
A: cells   6
A: jobs   6

output
Code:
A: cells  6
A: pens   4  
A: pens   4  B:Bags  4   total_count = 8
A: Apple  2  B:Bolls 4   total_count = 6 
A: jobs   6

But I am expecting 2nd line should not come as a output. it means I want output like below
Code:
A: pens   4  B:Bags  4   total_count = 8
A: Apple  2  B:Bolls 4   total_count = 6
A: cells  6
A: jobs   6

Thanks Advance

Last edited by pkkanduk; 02-02-2015 at 09:06 PM..
# 6  
Old 02-02-2015
So, at first you said you wanted to match on 6 fields. Then you wanted to match on 5 fields. And, now are you saying you only want to match on the 1st 3 fields? And:
Code:
A: jobs   6

is somehow magically dropped from the output?

Please explain in English what determines what lines are to be ignored, whether or not lines are considered to be a match, and when there are lines that match, which of the matching lines are to be printed.

Does the output order matter? If it does, please explain in English what the sort keys are.
# 7  
Old 02-02-2015
I want to ignore the specific lines which are matching 3 fields means
Code:
A: pens   4  
A: pens   4  B:Bags  4   total_count = 8 
A: cells  6 
A: jobs   6

above code line 1 and line 2 of 3 fields are same. When ever I saw this kind of pattern I want to remove a line which is having 3 fields are same. In the above example I want to remove a 1st line.

output which I am expecting
Code:
 A: pens   4  B:Bags  4   total_count = 8 
 A: cells  6 
 A: jobs   6


Last edited by pkkanduk; 02-02-2015 at 09:09 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with Sort in Unix

Hi All, I am trying to sort the below data using sort command. temp.dat H|S1-511091486889|27-Jul-2011 00:00:00 H|S1-511091486823|27-Jul-2011 00:00:00 H|S1-511091486757|27-Jul-2011 00:00:00 L|S1-511091486889|1 L|S1-511091486823|1 L|S1-511091486757|1 sort -t "|" -k2 -k1 temp.dat My... (5 Replies)
Discussion started by: deepaknbk
5 Replies

2. Shell Programming and Scripting

Sort with UNIX

I want to sort unique values of column 2 that has the maximum value at column 7. this is my file and desired output I have a file like this: AD008 AD0081010180947 101018 0947 0950 1010180947 1010180950 AD008 AD0081010180947 101018 0947 0956 1010180947 1010180956 AD008 AD0081010180947... (12 Replies)
Discussion started by: aydj
12 Replies

3. Shell Programming and Scripting

need Unix script to sort

Hi i have a file like this oprvdw vrc002093j.ksh oprvdw vrc002092j.ksh oprvrc vrc045016j.ksh oprvrc vrc055141j.ksh svemietl bdw0231185.sh svemietl bdw0231145.sh and i need a script which dispalys in below format: oprvdw : vrc002093j.ksh vrc002092j.ksh oprvrc :... (0 Replies)
Discussion started by: p_satyambabu
0 Replies

4. UNIX for Dummies Questions & Answers

UNIX sort command

Need some help with the sort command. I have a large file which needs sorted on the third field separated by : and within the third field, I need it sorted by second field or everything after the . An example of my file is here and for example, the first line I need :ROUTER2.SFLDMI: sorted on the... (2 Replies)
Discussion started by: numele
2 Replies

5. UNIX for Dummies Questions & Answers

using Unix sort command

Hi I am having some difficulties with the UNIX sort command. I want to sort one a file that looks like this (file A): tiger 5 6 3 5 2 bear 4 5 2 1 8 lions 9 2 5 3 1 dogs 8 5 3 3 1 acccording to a file that looks like this (file B): dogs lions tiger bear So... (2 Replies)
Discussion started by: phil_heath
2 Replies

6. UNIX for Dummies Questions & Answers

UNIX Sort question

I was trying to check for the sort of some columns (say 1-10) of particular file. Now, by default, the Unix sort uses as a separator whitespace (e.g. if you have 'foo bar' then it separates it into 'foo' and 'bar' to use as keys) Now, I know which particular columns I want to use as the sort... (1 Reply)
Discussion started by: rev.meister
1 Replies

7. UNIX for Dummies Questions & Answers

unix SORT

Hey guys. I am trying to sort a file by account number through UNIX. I have a few things but it seems to sort by account number AND sort everything after the account number. Help please. Thanks (5 Replies)
Discussion started by: ndoggy020
5 Replies

8. Shell Programming and Scripting

SORT order in Unix

I am converting mainframes JCL to be used in shell on a one to one basis... when i use the sort command unix does ascii sort as a result which numbers are first followed by charecters in the Ascending sort ... but themainframes uses the EBCDIC as result gives the charecters followed by numbers in... (5 Replies)
Discussion started by: bourne
5 Replies

9. UNIX for Dummies Questions & Answers

Unix Sort - Limitations

Hi All, I want to sort a flat file which will contain millions of records based on a key/field. For this I want to use unix sort command and before that I want to make sure that unix sort command has any file size limitations. And also please let me know whether I have to change any... (2 Replies)
Discussion started by: chprvkmr
2 Replies

10. UNIX for Advanced & Expert Users

Unix Sort - Alternatives

Hi All, I want to sort a flat file which will contain millions of records based on a key/field. For this I want to use unix sort command and before that I want to make sure that unix sort command has any file size limitations. And also please let me know whether I have to change any... (1 Reply)
Discussion started by: chprvkmr
1 Replies
Login or Register to Ask a Question