Questions tagged [sort]
Sorting text files or other data, for example with the sort utility.
1,036 questions
4
votes
1
answer
180
views
Choice of field separator affects sort's ordering
Suppose we have a script named test_sort in our $PATH with the following contents:
#!/bin/bash
function echo_text () {
printf -- "%s\n" "$fc$oc$fs$lc"
printf -- "%s\n&...
5
votes
0
answers
141
views
Why does Nautilus sort these files in this order?
In Nautilus (Debian 12, en_US.UTF-8, GNOME Files 43.2), the following eight files are sorted in the following order:
-
-1
--
--1
1
--a
-a
a
To answer the question (Why does Nautilus sort these files ...
4
votes
2
answers
609
views
Unix sort doesn't sort string correctly [duplicate]
Here is my simple test file:
# cat test
grp_pt cmg
grp_pt compsc
grp_pt ffre
grp_pt grp_pt
grp_pt hpcedt
grp_ptix dsnlp
grp_ptix exinspect
grp_ptix ptix
grp_ptix sciml
grp_ptix vers
grp_ptix ...
3
votes
1
answer
255
views
How are leading spaces handled in sort -k?
I have a file named test with the following content:
12288
4096
505
Note that there's a single space before 12288, two spaces before 4096 and three spaces before 505.
When I run GNU sort -k1.1,...
0
votes
1
answer
55
views
Sort of different column using different types, works, only strange thing with spaces
I have this file
1 you
3 me
2 their
4 us
this pipe works fine (it order first column by number, second by letters) is a pipe of another user found here
paste <(awk '{print $1}' file.txt | sort ...
0
votes
3
answers
133
views
Searching unique entries in a file
I am using Arch Linux/Debian Linux and want a list of unique "identifiers" in an ASCII txt file. Here is a snippet of the data I want to whittle down:
... (Received from VRW): wind ...
... (...
4
votes
1
answer
119
views
Why does command 'sort' behave differently for those 2 files
Those 2 sequences of lines are not sorted the same by the Linux shell command sort (gnu-coreutils 8.22 under RHEL 7.8), even though they begin with the same distinct prefixes. Why ?
$ echo "f'*.5....
1
vote
1
answer
116
views
How can I find duplicate lines among files?
I have a software module which contains some files with same pattern.
<whitespaces>private static final long serialVersionUID = \dL;
How can I find files with the same value?
$ grep -R ...
0
votes
2
answers
259
views
Sort is not sorting?
I seem to be having the same issue as described in The "join" utility reports: file is not sorted, but in fact it is sorted however I have piped BOTH files through sort before attempting to ...
10
votes
3
answers
2k
views
GNU sort command does not sort words of different lengths with common prefixes correctly when using field delimiter
The GNU sort command is not sorting words of different lengths with common prefixes correctly for me, but only when using a field delimiter to sort on one of multiple fields.
Here is the correct, ...
1
vote
1
answer
196
views
Why is sorted uniq -c command showing duplicates
I am trying to count how many times I use a certain version of a library on my computer.
For some reason, uniq -c is outputing duplicates, despite sorting it, and despite the sort order seeming in ...
1
vote
3
answers
856
views
How to sort files by size in bash script?
I want to loop over all files in directory on macOS and perform some operations on each file, and do this based on their size, going from big to small. To create a list I can loop over I do this:
...
6
votes
2
answers
1k
views
sort all arrays in a JSON file depth-first to get a consistent order
I'm tracking the configuration of some system in JSON format in a revision control system.
Unfortunately that configuration is retrieved using some closed-source proprietary command, and the output ...
3
votes
1
answer
121
views
Write header as first line at end of data
Having a read loop that manipulate data from another function.
One of the tasks in the loop is to record widest text in "some columns".
At the end of the loop the width is printed out, but ...
6
votes
3
answers
1k
views
awk: sort by first column then second; output unique 1st column once but all 2nd column
I have a two-column file that you can create as follows
cat > twocol << EOF
007 03
001 03
003 01
137 12
001 11
002 01
002 02
002 03
001 02
002 04
137 94
010 21
001 01
EOF
The resultant file, ...
0
votes
2
answers
54
views
Multilevel numeric path sort
I am on Ubuntu 22, and I have a large tree of files with annoying names that include numeric values in a variety of positions. Something like:
./2
./2/9
./2/10
./2/8
./1
./1/2
./1/1
./1/11
./11
./11/...
6
votes
3
answers
1k
views
How does sort -n command know what it needs to sort by in a file that contains both numbers and text?
I'm reading this blog at the moment: https://www.baeldung.com/linux/dir-find-largest-file-recursively
And I am asking about this command:
find /usr/lib -type f -printf "%s\t%p\n" | sort -n | ...
0
votes
1
answer
172
views
Display the results in an alphabetical order, independently of their case [duplicate]
I am using ls on MacOS 11.6.1. In the output, the Upper cases are displayed before the lower case. For example, I get the following output:
$ ls
Blabla.txt
GAGAS.txt
asdf.txt
blabla.txt
and would ...
4
votes
4
answers
572
views
How to sort the list of positional parameters in POSIX sh
Is there a way to sort the list of positional parameters in POSIX sh? Each positional parameter could contain arbitrary characters (e.g. spaces, newlines, tabs, etc.). The sort algorithm should be ...
1
vote
1
answer
780
views
How exactly does Nautilus sort files by name?
I noticed that Nautilus(GNOME Files) does not fully sort alphabetically.
$ touch 1
$ touch 2
$ touch 3
$ touch '!2'
Then the files are displayed in the following order: !2 1 2 3. This is fully ...
-2
votes
2
answers
181
views
Sorting a text file based on numerical values present in each line [closed]
I have a text file containing paths of various wav files. But when they are created they are sorted based on alphabetical order. Hence the paths are like this
/home/wav_path/...
1
vote
4
answers
288
views
How to sort and filter images by modification time and view them in nsxiv on FreeBSD
I am using nsxiv to view JPEG and PNG images on FreeBSD. However, I am unable to find a way to sort and filter the images by modification time to view them in nsxiv on FreeBSD. On Linux with GNU ...
0
votes
0
answers
19
views
reading into variables from "piped" data [duplicate]
I have a csv (Fieldseparator is the ',') file with data in it and I would have to prepare the csv before reading it (mainly just sort it) into variables. How can I read the sorted stream into ...
0
votes
1
answer
43
views
Pipe null terminated file paths twice to same output, but second time sorted by basename
So, let's say I have a script that uses find to print paths with null-terminated filenames.
I also want to print another version of the output where each path is sorted by its basename. I want to ...
3
votes
3
answers
411
views
MAC OSX / Bash Problem - Sorting strings as a date
I have file output in a CSV file I want to sort by date time but the date time appears as below, with the time and year appearing in a different order.
Using this command to produce the file listing:
...