Sort and Remove duplicates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort and Remove duplicates
# 1  
Old 02-16-2015
Sort and Remove duplicates

Here is my task :

I need to sort two input files and remove duplicates in the output files :
  1. Sort by 13 characters from 97 Ascending
  2. Sort by 1 characters from 96 Ascending

If duplicates are found retain the first value in the file

the input files are variable length, convert them 250 characters fixed width files with padding spaces.

Mainframe equivalent code :

https://www.unix.com/attachments/shel...tes-snipit-jpg

Here is the code i developed:

Code:
sort -nuc -k1.97,1.109 --key=1.96,1.96 file1 file2  | awk '{ printf ("%-250s\n",$0) }' > out.txt

Can any experts validate and correct me if something is wrong?
Sort and Remove duplicates-snipitjpg

Last edited by rbatte1; 02-17-2015 at 07:35 AM.. Reason: Converted to LIST=1 tags
# 2  
Old 02-17-2015
What OS and shell are you using?
What does your data look like?
  • Are there any spaces or tabs in the first 110 characters of any of your input lines?
  • What is the maximum line length of a line in your input files?
  • How big are your input files?
By definition, any lines that compare equal based on the sort key you provide are the same. When using the -u option, the sort utility makes no statement about which line from a set of lines having identical sort keys in the input files will be copied to the output.
# 3  
Old 02-17-2015
A few comments on your statement:

- the -c option would not sort:
Quote:
-c, --check, --check=diagnose-first
check for sorted input; do not sort
- as Don Cragun surmises, any white space before char 96 would count the fields up and destroy your key definitions. Set the terminator to an exotic char with -t
- you can use the short form -k more than one time in a statement
- if lines longer than 250 chars can occur (again DC'c suspicion), your printf format will expand the line; use the precision field as well: "%-250.250s\n"
# 4  
Old 02-17-2015
OS:
Linux x86_64 x86_64 x86_64 GNU/Linux

Sample Data :
Code:
YSVSR1 Kiladi    12198ASDA 21329180928AFJASDKDKDK ED AEFKF;p FK ADS 2132309183298209381 akfjalksdfjkdajfk j231 1239128390218309

Data contains spaces,tabs Alphanumeric values

Sort columns can also contain Alphanumeric Values

Are there any spaces or tabs in the first 110 characters of any of your input lines? Yes.

What is the maximum line length of a line in your input files? 211 Characters

How big are your input files? about 25000 lines in each file
# 5  
Old 02-19-2015
Quote:
Originally Posted by ysvsr1
OS:
Linux x86_64 x86_64 x86_64 GNU/Linux

Sample Data :
Code:
YSVSR1 Kiladi    12198ASDA 21329180928AFJASDKDKDK ED AEFKF;p FK ADS 2132309183298209381 akfjalksdfjkdajfk j231 1239128390218309

Data contains spaces,tabs Alphanumeric values

Sort columns can also contain Alphanumeric Values

Are there any spaces or tabs in the first 110 characters of any of your input lines? Yes.

What is the maximum line length of a line in your input files? 211 Characters

How big are your input files? about 25000 lines in each file
In your sample input, the first field is marked above in red. Since it contains less than 96 characters, every line in your input files will have the same, empty sort keys.

If you are trying to use the characters marked in orange above as your primary sort key (characters #97 through #109 on the line) and the character marked in green above as your secondary sort key (character #96 on the line), your sort keys would still all be identical because using the -n option to sort tells it to perform a numeric comparison and to stop trying to gather characters for a key at the first character that is not part of a numeric value. So, since characters #96 and #97 on your sample input line are both alphabetic, even if you change the field delimiter to something that does not appear anywhere in your input files, your sort keys will still all be 0 unless you remove the -n option.

And, as has been said before, you can't rely sort -u to get the unique keys, if you require that the 1st line be selected out of sets of lines with identical keys. (On some systems, that might happen to be what you get sometimes, but there is no guarantee that that is the line you'll always get.)

So, instead of showing us a sort command line that you know is not giving you what you want, please explain in English exactly what you are trying to do. And, explain what you want when you say "the input files are variable length, convert them 250 characters fixed width files with padding spaces." Do you want leading spaces to be added, or do you want trailing spaces to be added?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate and sort to remove duplicates

Following is the input. 1st and 3rd block are same(block starts here with '*' and ends before blank line) , 2nd and 4th blocks are also the same: cat <file> * Wed Feb 24 2016 Tariq Saeed <tariq.x.saeed@mail.com> 2.0.7-1.0.7 - add vmcore dump support for ocfs2 * Mon Jun 8 2015 Brian Maly... (4 Replies)
Discussion started by: Paras Pandey
4 Replies

2. UNIX for Beginners Questions & Answers

Sort and remove duplicates in directory based on first 5 columns:

I have /tmp dir with filename as: 010020001_S-FOR-Sort-SYEXC_20160229_2212101.marker 010020001_S-FOR-Sort-SYEXC_20160229_2212102.marker 010020001-S-XOR-Sort-SYEXC_20160229_2212104.marker 010020001-S-XOR-Sort-SYEXC_20160229_2212105.marker 010020001_S-ZOR-Sort-SYEXC_20160229_2212106.marker... (4 Replies)
Discussion started by: gnnsprapa
4 Replies

3. Shell Programming and Scripting

Help in modifying a PERL script to sort Singletons and Duplicates

I have a large database which has the following structure a=b where a is one language and b is the other and = is the delimiter Since the data treats of language, homographs occur i.e. the same word on the left hand side can map in two different entries to two different glosses on the right... (3 Replies)
Discussion started by: gimley
3 Replies

4. Shell Programming and Scripting

Bash - remove duplicates without sort

I need to use bash to remove duplicates without using sort first. I can not use: cat file | sort | uniq But when I use only cat file | uniq some duplicates are not removed. (4 Replies)
Discussion started by: locoroco
4 Replies

5. Shell Programming and Scripting

Sort data by date first and then remove duplicates

Hi , I have below data inside a file named ref.psv . I want to create a shell script which will do the below 2 points : (1) sort the file content first based on the latest date which is the last column in the file (actual file its the 175th column) (2)after sorting the file based on latest date... (3 Replies)
Discussion started by: samrat dutta
3 Replies

6. Shell Programming and Scripting

remove duplicates and sort

Hi, I'm using the below command to sort and remove duplicates in a file. But, i need to make this applied to the same file instead of directing it to another. Thanks (6 Replies)
Discussion started by: dvah
6 Replies

7. UNIX for Dummies Questions & Answers

sort and find duplicates for files with no white space

example data 5666700842511TAfmoham03151008075205999900000001000001000++ 5666700843130MAfmoham03151008142606056667008390315100005001 6666666663130MAfmoham03151008142606056667008390315100005001 I'd like to sort on position 10-14 where the characters are eq "130MA". Then based on positions... (0 Replies)
Discussion started by: mmarshall
0 Replies

8. UNIX for Dummies Questions & Answers

removing duplicates and sort -k

Hello experts, I am trying to remove all lines in a csv file where the 2nd columns is a duplicate. I am try to use sort with the key parameter sort -u -k 2,2 File.csv > Output.csv File.csv File Name|Document Name|Document Title|Organization Word Doc 1.doc|Word Document|Sample... (3 Replies)
Discussion started by: orahi001
3 Replies

9. Shell Programming and Scripting

Sort, Uniq, Duplicates

Input File is : ------------- 25060008,0040,03, 25136437,0030,03, 25069457,0040,02, 80303438,0014,03,1st 80321837,0009,03,1st 80321977,0009,03,1st 80341345,0007,03,1st 84176527,0047,03,1st 84176527,0047,03, 20000735,0018,03,1st 25060008,0040,03, I am using the following in the script... (5 Replies)
Discussion started by: Amruta Pitkar
5 Replies

10. Shell Programming and Scripting

Removing duplicates [sort , uniq]

Hey Guys, I have file which looks like this, Contig201#numbPA Contig1452#nmdynD6PA dm022p15.r#CG6461PA dm005e16.f#SpatPA IGU001_0015_A06.f#CG17593PA I need to remove duplicates based on the chracter matching upto '#'. for example if we consider this.. Contig201#numbPA... (4 Replies)
Discussion started by: sharatz83
4 Replies
Login or Register to Ask a Question