Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Inconsistent results using sort function Post 303027265 by aberg on Wednesday 12th of December 2018 04:17:55 PM
Old 12-12-2018
Inconsistent results using sort function

Could you please advise on the following: I have two space-delimited files with 9 and 10 columns, respectively, with exactly the same values in column 1. However, the order of column 1 differs between the two files, so I want to sort both files by column 1, so that I can align them and concatenate them into a 19 column file.


If I want to sort by column 1, I usually use "sort -k 1,1 -g". I have done this hundreds of times and I have never had a problem with it.
This is the first time ever that the sort function has given a different output for the two files, despite using identical commands:

Code:
sort -k 1,1 -g file1.txt | head

rs1000000 12 126890980 G A 0.772687 0.999152 -6.53289e-05 0.000341777  
rs10000003 4 57561647 A G 0.298872 0.997534 -0.000308206 0.000313536 
rs10000005 4 85161558 G A 0.468352 0.994261 0.000392384 0.000287513 
rs10000010 4 21618674 T C 0.517001 0.986406 -0.000387116 0.000288364  
rs10000011 4 138223055 C T 0.957162 0.987603 -0.000466108 0.000710431  
rs10000012 4 1357325 C G 0.85952 0.999131 -0.000544182 0.000412222  
rs10000017 4 84778125 C T 0.777348 0.989758 0.00024644 0.000345697  
rs10000018 4 100458448 A G 0.707724 0.999129 -5.96813e-05 0.000315027  
rs10000021 4 159441457 G T 0.185355 0.99682 0.000127756 0.000369005  
rs1000002 3 183635768 C T 0.513401 1 -0.000269255 0.000286993 3.5E-01

and

Code:
sort -k 1,1 -g file2.txt | head 

rs10000003 G A 0.707825 1.010846 0.015580 0.980310 1.042333 0.490663
rs10000005 A G 0.550104 0.988740 0.014283 0.960744 1.017551 0.439681
rs1000000 G A 0.780117 0.987172 0.017380 0.953108 1.022454 0.471168 
rs10000010 C T 0.503288 1.009101 0.014611 0.980464 1.038574 0.537391 
rs10000011 C T 0.950554 0.997444 0.026380 0.945740 1.051976 0.924913 
rs10000012 C G 0.866931 0.966905 0.021645 0.924482 1.011276 0.141498 
rs10000017 C T 0.791953 1.003966 0.019870 0.965021 1.044483 0.844517 
rs10000018 A G 0.699162 1.006137 0.014434 0.977846 1.035245 0.674194 
rs10000021 T G 0.827782 0.991092 0.021206 0.949529 1.034474 0.682292 
rs10000023 T G 0.579281 1.024738 0.014014 0.997270 1.052962 0.077937

Why is this happening despite identical commands? I'm especially puzzled because I have never encountered this before.

Thank you for any advice.

aberg
 

10 More Discussions You Might Find Interesting

1. Programming

sort function

do any one knows where i can find an implementation in c for the sort function (2 Replies)
Discussion started by: dbargo
2 Replies

2. UNIX for Dummies Questions & Answers

How to sort find results

Hi-- Ok. I have now found that: find -x -ls will do what I need as far as finding all files on a particular volume. Now I need to sort the results by the file's modification date/time. Is there a way to do that? Also, I notice that for many files, whereas the man for find says ls is... (8 Replies)
Discussion started by: groundlevel
8 Replies

3. Shell Programming and Scripting

sort function in perl

Hi, here is my perl script.This script creates an array and is sorting it using the in-built sort function in perl. #!/usr/local/bin/perl my number=6; my @num_arr=(1,2,3,4,5); my @array=(23,"$number","Hello",2.345,@num_arr); #printing the array print... (2 Replies)
Discussion started by: DILEEP410
2 Replies

4. Shell Programming and Scripting

HELP: I need to sort a text file in an uncommon manner, can't get desired results

Hi All I have a flat text file. Each line in it contains a "/full path/filename". The last three columns are predictable, but directory depth of each line varies. I want to sort on the last three columns, starting from the last, 2nd last and 3rd last. In that order. The last three columns... (6 Replies)
Discussion started by: JakeKatz
6 Replies

5. Shell Programming and Scripting

Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!! I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively. Now the problem is that it works well if i use it with one time but the problem is that when... (0 Replies)
Discussion started by: anthriksh2000
0 Replies

6. Shell Programming and Scripting

Help with sort folder results

Here is the code, but the list is not sorted properly (alphabetically)? <?php function folderlist(){ $startdir = './'; $ignoredDirectory = '.'; $ignoredDirectory = '..'; if (is_dir($startdir)){ if ($dh = opendir($startdir)){ while (($folder = readdir($dh)) !== false){ if... (0 Replies)
Discussion started by: mrlayance
0 Replies

7. UNIX for Dummies Questions & Answers

sort find results

Hi, I have a problem with a shell script. The script should find all .cpp and .h files and list them. With: for file in `find $src -name '*.h' -o -name '*.cpp' it gives out this: H:\FileList\A\E\F\G\newCppFile.cpp H:\FileList\header01.h H:\FileList\B\nextCppFile.cpp ... (4 Replies)
Discussion started by: shellBeginner75
4 Replies

8. UNIX for Dummies Questions & Answers

LINUX SORT command chops results

I am trying to sort a file . The file looks like this: DDFF 2 /ztpfrepos/pgr/load DDFQ 2 /ztpfrepos/pgr/load DDFX 2 /ztpfrepos/pgr/load DDUA 2 /ztpfrepos/pgr/load My command: sort -k1 /home/c153507/Bin/OPL1.txt -o /home/c153507/Bin/OPL1.txt The results are OK except for one line where... (4 Replies)
Discussion started by: Yahalom
4 Replies

9. UNIX for Beginners Questions & Answers

Strange sort -r results

Hi Folks - I have this file that looks like this: outbox/logs/Client_1042.log outbox/logs/Client_941.log outbox/logs/Client_942.log outbox/logs/Client_943.log outbox/logs/Client_944.log And this is my code: #!/bin/bash _OUTBOX_BIN="outbox/logs/" _NAME="Client" _TEMP="temp.txt"... (9 Replies)
Discussion started by: SIMMS7400
9 Replies

10. UNIX for Beginners Questions & Answers

Strange results from 'strings | sort'

Using the 'strings' command and piping the result to 'sort' is producing strange results. I get block of lines that begin with asterisks, then a block that begins with some text, then more lines that begin with asterisks. The actual content is correct - lines beginning with asterisks is the... (5 Replies)
Discussion started by: edstevens
5 Replies
JOIN(1) 						      General Commands Manual							   JOIN(1)

NAME
join - relational database operator SYNOPSIS
join [ options ] file1 file2 DESCRIPTION
Join forms, on the standard output, a join of the two relations specified by the lines of file1 and file2. If one of the file names is the standard input is used. File1 and file2 must be sorted in increasing ASCII collating sequence on the fields on which they are to be joined, normally the first in each line. There is one line in the output for each pair of lines in file1 and file2 that have identical join fields. The output line normally con- sists of the common field, then the rest of the line from file1, then the rest of the line from file2. Input fields are normally separated spaces or tabs; output fields by space. In this case, multiple separators count as one, and leading separators are discarded. The following options are recognized, with POSIX syntax. -a n In addition to the normal output, produce a line for each unpairable line in file n, where n is 1 or 2. -v n Like -a, omitting output for paired lines. -e s Replace empty output fields by string s. -1 m -2 m Join on the mth field of file1 or file2. -jn m Archaic equivalent for -n m. -ofields Each output line comprises the designated fields. The comma-separated field designators are either 0, meaning the join field, or have the form n.m, where n is a file number and m is a field number. Archaic usage allows separate arguments for field designators. -tc Use character c as the only separator (tab character) on input and output. Every appearance of c in a line is significant. EXAMPLES
sort /adm/users | join -t: -a 1 -e "" - bdays Add birthdays to password information, leaving unknown birthdays empty. The layout of is given in users(6); bdays contains sorted lines like tr : ' ' </adm/users | sort -k 3 3 >temp join -1 3 -2 3 -o 1.1,2.1 temp temp | awk '$1 < $2' Print all pairs of users with identical userids. SOURCE
/sys/src/cmd/join.c SEE ALSO
sort(1), comm(1), awk(1) BUGS
With default field separation, the collating sequence is that of sort -b -ky,y; with -t, the sequence is that of sort -tx -ky,y. One of the files must be randomly accessible. JOIN(1)
All times are GMT -4. The time now is 02:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy