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
comm(1) 						      General Commands Manual							   comm(1)

NAME
comm - Compares two sorted files. SYNOPSIS
comm [-123] file1 file2 STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: command: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
Suppresses output of the first column (lines in file1 only). Suppresses output of the second column (lines in file2 only). Suppresses output of the third column (lines common to file1 and file2). The command comm -123 produces no output. OPERANDS
A pathname of the first file to be compared. If file1 is a hyphen (-), the standard input is used. A pathname of the second file to be compared. If file2 is a hyphen (-), the standard input is used. If both file1 and file2 refer to standard input or to the same FIFO special, block special or character special file, the results are unde- fined. DESCRIPTION
The comm command reads file1 and file2 and writes three columns to standard output, showing which lines are common to the files and which are unique to each. The leftmost column of standard output includes lines that are in file1 only. The middle column includes lines that are in file2 only. The rightmost column includes lines that are in both file1 and file2. If you specify a hyphen (-) in place of one of the file names, comm reads standard input. Generally, file1 and file2 should be sorted according to the collating sequence specified by the LC_COLLATE environment variable. (See sort(1).) If the input files are not sorted properly, the output of comm might not be useful. EXIT STATUS
Successful completion. Error occurred. EXAMPLES
In the following examples, file1 contains the following sorted list of North American cities: Anaheim Baltimore Boston Chicago Cleveland Dallas Detroit Kansas City Milwaukee Minneapolis New York Oakland Seattle Toronto The second file, file2, contains this sorted list: Atlanta Chicago Cincinnati Houston Los Angeles Montreal New York Philadelphia Pittsburgh San Diego San Francisco St. Louis To display the lines unique to each file and common to the two files, enter: comm file1 file2 This command results in the following output: Anaheim Atlanta Baltimore Boston Chicago Cincinnati Cleveland Dal- las Detroit Houston Kansas City Los Angeles Milwaukee Minneapolis Montreal New York Oakland Philadel- phia Pittsburgh San Diego San Francisco Seattle St. Louis Toronto The leftmost column contains lines in file1 only, the middle column contains lines in file2 only, and the rightmost column contains lines common to both files. To display any one or two of the three output columns, include the appropriate flags to suppress the columns you do not want. For example, the following command displays columns 1 and 2 only: comm -3 file1 file2 Anaheim Atlanta Baltimore Boston Cincinnati Cleveland Dallas Detroit Houston Kansas City Los Angeles Milwaukee Minneapolis Montreal Oakland Philadelphia Pittsburgh San Diego San Francisco Seattle St. Louis Toronto The following command displays output from only the second column: comm -13 file1 file2 Atlanta Cincinnati Houston Los Angeles Montreal Philadelphia Pittsburgh San Diego San Francisco St. Louis The following command displays output from only the third column: comm -12 file1 file2 Chicago New York SEE ALSO
Commands: cmp(1), diff(1), sdiff(1), sort(1), uniq(1) comm(1)
All times are GMT -4. The time now is 06:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy