Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
120
votes
4
answers
54341
views
Does sort support sorting a file in-place, like `sed --in-place`?
Is there no option like `--in-place` for `sort`? In order to save results to the input file, sed uses `-i` (`--in-place`). Redirecting the output of `sort` to the input file sort f results in making it empty. If there is no `--in-place` option - maybe there is some trick how to do this in *handy* wa...
Is there no option like
--in-place
for sort
?
In order to save results to the input file, sed uses -i
(--in-place
).
Redirecting the output of sort
to the input file
sort f
results in making it empty. If there is no --in-place
option - maybe there is some trick how to do this in *handy* way?
(The only thing that cames to my mind:
sort /tmp/f$$ ; cat /tmp/f$$ > f ; rm /tmp/f$$
Moving is not right choice, cause *file permissions* might be changed. That's why I overwrite with the contents of the temp file which I then remove.)
Grzegorz Wierzowiecki
(14740 rep)
Jan 22, 2012, 10:02 AM
• Last activity: Aug 4, 2025, 01:30 PM
0
votes
1
answers
56
views
sort result of fd
I use [fd][1] to find file names containing only zeros. I want adopt this [code][2] to work with all files in folders, not with the only one file... This works not bad $ fdfind -tf -x bash -c '[ -s {} ] && (tr -d "\0" <{} | grep -q -m 1 ^ || (printf "{} " && wc -c <{}))' ./24.21.bin 1024 ./24.2.bin...
I use fd to find file names containing only zeros. I want adopt this code to work with all files in folders, not with the only one file... This works not bad
$ fdfind -tf -x bash -c '[ -s {} ] && (tr -d "\0" <{} | grep -q -m 1 ^ || (printf "{} " && wc -c <{}))'
./24.21.bin 1024
./24.2.bin 1024
./24.20.bin 1024
(Where
[ -s {} ] &&
is to check that file is 0 bytes and exclude it).
But I don't want alphabetical order, I want Natural sort . I can try to use -X
option of fd
or try to use pipe, this is draft with pipe
$ fdfind -tf | sort --version-sort | xargs -d '\n'
24.2.bin 24.20.bin 24.21.bin
Sorting is natural, next steps should be 1) add all-zeros check 2) print filename. How to do it? For example this code contains all-zeros check (with result in $?
), but I don't know how to print filename, so this code print nothing...
fdfind -tf | sort --version-sort | xargs -d '\n' grep -qavE '^(00)*$'
P.S. To test my code you need all-zeros files, generate it using
truncate -s 1K 24.2.bin
truncate -s 1K 24.20.bin
truncate -s 1K 24.21.bin
Андрей Тернити
(303 rep)
Jul 22, 2025, 06:43 AM
• Last activity: Jul 22, 2025, 07:32 AM
0
votes
4
answers
2711
views
Find out which users are hogging the most disk space on our data server
We are supposed to store our ongoing projects on a rather small (~4TB) data server. Not surprisingly, it is constantly overflowing and people need to move off less recent files manually. Is there an easy (aka standard command-line) way to find out which users take up the most space in a directory? i...
We are supposed to store our ongoing projects on a rather small (~4TB) data server. Not surprisingly, it is constantly overflowing and people need to move off less recent files manually.
Is there an easy (aka standard command-line) way to find out which users take up the most space in a directory? i.e. summing up the size of all files in a directory and all sub-directories belonging to each user?
Edit: ideally not following symlinks
MechEng
(233 rep)
Mar 26, 2019, 07:39 AM
• Last activity: Jul 11, 2025, 05:04 PM
4
votes
2
answers
554
views
Unix sort doesn't sort string correctly
Here is my simple test file: ```shell # 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 vsme grp_pt mhm grp_pt rec grp_pt recom grp_ptix grp_ptix grp_ptix iai grp_ptix mv grp_ptix pid ```...
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 vsme
grp_pt mhm
grp_pt rec
grp_pt recom
grp_ptix grp_ptix
grp_ptix iai
grp_ptix mv
grp_ptix pid
# sort -u 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 grp_ptix
grp_ptix iai
grp_ptix mv
grp_ptix pid
grp_ptix ptix
grp_ptix sciml
grp_ptix vers
grp_ptix vsme
grp_pt mhm
grp_pt rec
grp_pt recom
I expected the last 3 line should be together and right below of the first 5 lines. But it seems not doing that way.
Any suggestion?
Thu-Ha Tran
(41 rep)
Jun 25, 2025, 02:07 PM
• Last activity: Jun 26, 2025, 10:45 AM
6
votes
1
answers
290
views
Strange default sort behaviour
I have some trouble to understand what is happening here: [guido@localhost 9]$ ls -1 Star\ Wars\ Episode\ * Star Wars Episode II Attack of the Clones.avi Star Wars Episode III Revenge of the Sith.avi Star Wars Episode I The Phantom Menace.avi Star Wars Episode IV A New Hope.avi Star Wars Episode VI...
I have some trouble to understand what is happening here:
[guido@localhost 9]$ ls -1 Star\ Wars\ Episode\ *
Star Wars Episode II Attack of the Clones.avi
Star Wars Episode III Revenge of the Sith.avi
Star Wars Episode I The Phantom Menace.avi
Star Wars Episode IV A New Hope.avi
Star Wars Episode VI Return of the Jedi.avi
Star Wars Episode V The Empire Strikes Back.avi
III\b
before I\b
, but II\b
before III\b
? Whatever is causing this, it is not behaving consistently. This is also the same sorting result I get in the GUI. I checked all the blanks are actually blanks, and there are no case difference among the filenames. How can it be? Is it skipping the roman numeral, and trying to sort after it?
Other tests:
[guido@localhost 9]$ find -name "Star Wars *" -print
./Star Wars Episode I The Phantom Menace.avi
./Star Wars Episode II Attack of the Clones.avi
./Star Wars Episode III Revenge of the Sith.avi
./Star Wars Episode IV A New Hope.avi
./Star Wars Episode V The Empire Strikes Back.avi
./Star Wars Episode VI Return of the Jedi.avi
But:
[guido@localhost 9]$ find -name "Star Wars *" -print | sort
./Star Wars Episode II Attack of the Clones.avi
./Star Wars Episode III Revenge of the Sith.avi
./Star Wars Episode I The Phantom Menace.avi
./Star Wars Episode IV A New Hope.avi
./Star Wars Episode VI Return of the Jedi.avi
./Star Wars Episode V The Empire Strikes Back.avi
While:
[guido@localhost 9]$ find -name "Star Wars *" -print | sort -f
./Star Wars Episode I The Phantom Menace.avi
./Star Wars Episode II Attack of the Clones.avi
./Star Wars Episode III Revenge of the Sith.avi
./Star Wars Episode IV A New Hope.avi
./Star Wars Episode V The Empire Strikes Back.avi
./Star Wars Episode VI Return of the Jedi.avi
I don't think my locale may be affecting this (I also set it to en_US.utf8 anyway) as the doc suggests. What am I missing?
[guido@localhost 9]$ sort --version
sort (GNU coreutils) 8.22
guido
(9059 rep)
Oct 10, 2015, 04:22 PM
• Last activity: Jun 26, 2025, 07:34 AM
1
votes
0
answers
66
views
How can I find multiple duplicates of media files,sort, backup them and delete the rest?
I have a 4 TB hard drive containing pictures, sounds, and videos from the last 15 years. These files were copied onto this drive from various sources, including hard drives, cameras, phones, CD-ROMs, DVDs, USB sticks, SD cards, and downloads. The files come in formats such as JPEG, PNG, GIF, SVG, VO...
I have a 4 TB hard drive containing pictures, sounds, and videos from the last 15 years.
These files were copied onto this drive from various sources, including hard drives, cameras, phones, CD-ROMs, DVDs, USB sticks, SD cards, and downloads.
The files come in formats such as JPEG, PNG, GIF, SVG, VOB, MP4, MPEG, MOV, AVI, SWF, WMV, FLV, 3GP, WAV, WMA, AAC, and OGG. Over the years, the files have been copied back and forth between different file systems, including FAT, exFAT, NTFS, HFS+/APFS, and ext3/ext4. Currently, the hard drive uses the ext4 file system.
There are folders and files that appear multiple times (duplicates, triplicates, or even more).
The problem is that the folder and filenames are not always identical.
For example:
1. A folder named "bilder_2012" might appear elsewhere as "backup_bilder_2012" or "media_2012_backup_2016".
2. In some cases, newer folders contain additional files that were not present in the older versions.
3. The files themselves may have inconsistent names, such as "bild1", "bild2" in one folder and "bilder2018(1)", "bilder2018(2)" in another.
What I Want to Achieve:
1. Sort and clean up the files and Remove all duplicates and copy the remaining files to a new hard drive.
2. Identify the original copies and Is there a way to determine which version of a file is the earliest/original?
3. Preserve the original folder names and For example, I know that "bilder_2012" was the first name given to a folder, and I like to keep that name if possible.
4. Standardize file naming and After copying, I like the files to follow a consistent naming scheme, such as.
Folder: "bilder2012" and Files: "bilder2012(1).jpeg", "bilder2012(2).jpeg", etc.
Is there a way to automate this process while ensuring the oldest/original files are preserved and duplicates are safely removed?
Bernd Kunze
(11 rep)
Jun 21, 2025, 09:49 AM
• Last activity: Jun 25, 2025, 07:26 AM
3
votes
1
answers
209
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,1.3 test` on my Ubuntu system, I get the following output: ``` 505 12288 4096 ``` I expected it...
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,1.3 test
on my Ubuntu system, I get the following output:
505
12288
4096
I expected it to do a lexicographic sorting on the first three characters (including spaces) based on their ASCII values which would have produced the following output:
505
4096
12288
Running =C sort -k1.1,1.3 test
gives me the expected output. Here's the output I get when I run
:
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
Could you please explain what the command is doing when I don't pass LC_ALL=C to it?
russell.price
(53 rep)
Apr 29, 2025, 04:26 PM
• Last activity: Apr 29, 2025, 07:51 PM
5
votes
2
answers
159
views
How to show directory sizes in tree format with correct values and depth control
I can't solve what I want using the `tree`, `du`, or `df` commands. I'm using: - `du` version 9.6 - `tree` version 2.2.1 **What I want**: For the current directory, list all directories recursively to a depth of 3, showing their size and ordering them by size. For clarity, each level of subdirectory...
I can't solve what I want using the
tree
, du
, or df
commands.
I'm using:
- du
version 9.6
- tree
version 2.2.1
**What I want**:
For the current directory, list all directories recursively to a depth of 3, showing their size and ordering them by size.
For clarity, each level of subdirectory ought to be indented.
Using du -h -d 3
does not provide a tree view.
Now, tree
seems to be the command to use, but I do not understand the output, so I made a test directory and here are the outputs.
In the following, I do not understand the answer with
- tree --du -h -L 2
for the directory size.
- tree --du -h -L 3
for the directory size of immo2
as is 16M and not 4.0K, as well as immo
and root size 102M and not 86M.
Remark:
tree --du -h -L 2
and tree -du -h -L 2
do not provide the same output.
Thanks.
$ tree --du -h -L 3
[ 86M] .
└── [ 86M] sub1
├── [3.6M] immo
│ ├── [ 670] 00Tree.html
│ ├── [416K] 2013-08-27_120009.jpg
│ ├── [3.2M] 2014-04-09_093940.jpg
│ └── [4.0K] immo_2
└── [ 82M] ogg
├── [ 141] 00Tree.html
└── [ 82M] 2020-06-21.ogg
86M used in 5 directories, 5 files
$
$ tree --du -h -L 2
[4.0K] .
└── [4.0K] sub1
├── [4.0K] immo
└── [ 0] ogg
4.0K used in 4 directories, 0 files
$
$ du -h -d 3
16M ./sub1/immo/immo_2
20M ./sub1/immo
83M ./sub1/ogg
102M ./sub1
102M .
$
P.S. In the meantime,
1/ filed a bug report for tree cmd.
2/ I found an awesome app: https://apps.kde.org/fr/filelight/
user2718593
(69 rep)
Apr 11, 2025, 12:18 PM
• Last activity: Apr 22, 2025, 09:03 AM
33
votes
4
answers
34533
views
What's the default order of Linux sort?
For a long period I thought the default behavior of the `sort` program was using ASCII order. However, when I input the following lines into `sort` without any arguments: # @ I got: @ # But according to the ASCII table, `#` is 35 and `@` is 64. Another example is: A a And the output is: a A Can anyb...
For a long period I thought the default behavior of the
sort
program was using ASCII order. However, when I input the following lines into sort
without any arguments:
#
@
I got:
@
#
But according to the ASCII table, #
is 35 and @
is 64. Another example is:
A
a
And the output is:
a
A
Can anybody explain this? By the way, what is 'dictionary-order' when using sort -d
?
old_bear
Jul 19, 2012, 06:16 AM
• Last activity: Apr 8, 2025, 03:02 PM
1
votes
4
answers
1398
views
Sorting files based on their extension and moving them to their directories
I have a script that takes in all the different types of extensions in a file and creates a directory from them. However, I need to create a directory for only 3 types of extensions. JPG/JPEG, DOC/DOCX, and 1 directory with other types of extensions "miscellaneous". This is my script so far. ```sh #...
I have a script that takes in all the different types of extensions in a file and creates a directory from them.
However, I need to create a directory for only 3 types of extensions. JPG/JPEG, DOC/DOCX, and 1 directory with other types of extensions "miscellaneous".
This is my script so far.
#!/bin/bash
exts=$(ls | sed 's/^.*\.//' | sort -u)
for ext in $exts; do
mkdir $ext
mv -v *.$ext $ext/
done
GlassDow
(13 rep)
Feb 17, 2021, 12:30 PM
• Last activity: Apr 2, 2025, 10:21 PM
2
votes
2
answers
271
views
How does "sort -k" handle missing columns?
While the `-k` option in the `sort` command helps to sort data by a given column, the example below, which is not perfectly tabular, is still sorted by the column. How does it do that? Which character does it assume in place of empty cells? ```shell $ cat file1 a b c d e ``` ```shell $ sort -k 2 fil...
While the
-k
option in the sort
command helps to sort data by a given column, the example below, which is not perfectly tabular, is still sorted by the column. How does it do that? Which character does it assume in place of empty cells?
$ cat file1
a b c
d
e
$ sort -k 2 file1
d
e
a b c
Number945
(173 rep)
Jan 28, 2021, 07:49 PM
• Last activity: Mar 14, 2025, 08:05 PM
0
votes
1
answers
3719
views
Using GNU sort with multiple keys, text and numbers
I have some mail log excerpts that I'd like to sort first by e-mail address and then by date. Example input data: $ cat test3.txt Oct 10 14:00:00 andy@example.com bounced Oct 10 13:00:00 andy@example.com deferred Oct 10 14:30:00 billy@example.com bounced Oct 10 12:00:00 andy@example.com deferred Oct...
I have some mail log excerpts that I'd like to sort first by e-mail address and then by date.
Example input data:
$ cat test3.txt
Oct 10 14:00:00 andy@example.com bounced
Oct 10 13:00:00 andy@example.com deferred
Oct 10 14:30:00 billy@example.com bounced
Oct 10 12:00:00 andy@example.com deferred
Oct 9 12:00:00 cindy@example.com deferred
Oct 9 14:00:00 cindy@example.com bounced
Oct 10 12:30:00 billy@example.com deferred
Oct 10 13:30:00 billy@example.com deferred
Oct 9 13:00:00 cindy@example.com deferred
The file in its current version is space delimited. So what I want is to sort first by the fourth column, and then by the first (as month), second (numerical) and third (numerical, I guess, unless the timestamps need special handling.) This is my best attempt:
$ sort -k 4,4 -k 1,1M -nk 2 test3.txt
Oct 9 12:00:00 cindy@example.com deferred
Oct 9 13:00:00 cindy@example.com deferred
Oct 9 14:00:00 cindy@example.com bounced
Oct 10 12:00:00 andy@example.com deferred
Oct 10 12:30:00 billy@example.com deferred
Oct 10 13:00:00 andy@example.com deferred
Oct 10 13:30:00 billy@example.com deferred
Oct 10 14:00:00 andy@example.com bounced
Oct 10 14:30:00 billy@example.com bounced
If I include only the "-k 4,4" key argument, it sorts fine according to the e-mail but that seems to get ignored when I add the other keys. For simplicity the first column can be ignored in this example; the problem is still there in that the sorting by the second column takes precedence over the fourth.
What am I doing wrong?
anlag
(123 rep)
Oct 24, 2016, 10:20 AM
• Last activity: Mar 11, 2025, 04:06 AM
1
votes
5
answers
494
views
Sorting elements of a CSV file
I have a csv file with seven numbers per line like this: 1083,20,28,42,23,10,43 1084,20,5,29,59,40,33 1085,39,50,21,12,40,55 1086,45,4,6,23,10,2 1087,36,46,28,32,3,20 I want to keep the first number in place (column 1) and sort columns 2 to 7, making the file like 1083,10,20,23,28,42,43 1084,5,20,29...
I have a csv file with seven numbers per line like this:
1083,20,28,42,23,10,43
1084,20,5,29,59,40,33
1085,39,50,21,12,40,55
1086,45,4,6,23,10,2
1087,36,46,28,32,3,20
I want to keep the first number in place (column 1) and sort columns 2 to 7, making the file like
1083,10,20,23,28,42,43
1084,5,20,29,33,40,59
1085,12,21,39,40,50,55
1086,2,4,6,10,45,23
1087,3,20,28,32,36,46
How can I do that with
awk
, sed
or whatever?
Thanks
Duck
(4794 rep)
Feb 26, 2019, 10:03 AM
• Last activity: Mar 10, 2025, 10:35 PM
229
votes
11
answers
185994
views
List files sorted numerically
I have a bunch of files from `log1` to `log164`. I'm trying to LIST the directory (sorted) in a UNIX terminal but the sort functions are only providing the format like this: home:logs Home$ ls -1 | sort log1.gz log10.gz log100.gz log101.gz log102.gz log103.gz log104.gz log105.gz log106.gz ...etc Wha...
I have a bunch of files from
log1
to log164
.
I'm trying to LIST the directory (sorted) in a UNIX terminal but the sort functions are only providing the format like this:
home:logs Home$ ls -1 | sort
log1.gz
log10.gz
log100.gz
log101.gz
log102.gz
log103.gz
log104.gz
log105.gz
log106.gz
...etc
What I want is
home:logs Home$ ls -1 | sort
log1.gz
log2.gz
log3.gz
log4.gz
log5.gz
log6.gz
log7.gz
...{more here}
log99.gz
log100.gz
log101.gz
log102.gz
...etc
Any suggestions in what I could use to do this?
Rabiani
Mar 9, 2012, 02:18 AM
• Last activity: Mar 7, 2025, 02:38 PM
3
votes
3
answers
648
views
Tool for viewing top N items in stream
I'm looking for an existing Linux command-line tools that can accomplish the following: Consider there is an input stream `/dev/inputstream` which spews an infinite stream of data that looks like this: A A B A C Z A . . . **NOTE:** (Each line is one character from the alphabet). The tool is used in...
I'm looking for an existing Linux command-line tools that can accomplish the following:
Consider there is an input stream
/dev/inputstream
which spews an infinite stream of data that looks like this:
A
A
B
A
C
Z
A
.
.
.
**NOTE:** (Each line is one character from the alphabet).
The tool is used in the following fashion:
cat /dev/inputstream | tool
The output of the tool should be like the one from "top" command. It should be a continuously updated list of top N frequently occurring items.
Eg:
A -- 10 times
B -- 8 times
Z -- 7 times
C -- 2 times
D -- 1 time
If the stream were not infinite I could have accomplished this using sort
and uniq
.
Prashanth Ellina
(199 rep)
May 15, 2014, 06:18 PM
• Last activity: Mar 5, 2025, 06:33 AM
6
votes
2
answers
6475
views
Sorting a file based on one column using Unix and Awk
I need to sort the input file according the 6th column, which is the score. **Input File:** Sc2/80 20 . A T 86 Pass N=2 F=5;U=4 Sc2/80 20 . A C 80 Pass N=2 F=5;U=4 Sc2/60 55 . G T 90 Pass N=2 F=5;U=4 Sc2/60 55 . G C 99 Pass N=2 F=5;U=4 Sc2/20 39 . C T 97 Pass N=2 F=5;U=4 Sc2/20 39 . C A 99 Pass N=2...
I need to sort the input file according the 6th column, which is the score.
**Input File:**
Sc2/80 20 . A T 86 Pass N=2 F=5;U=4
Sc2/80 20 . A C 80 Pass N=2 F=5;U=4
Sc2/60 55 . G T 90 Pass N=2 F=5;U=4
Sc2/60 55 . G C 99 Pass N=2 F=5;U=4
Sc2/20 39 . C T 97 Pass N=2 F=5;U=4
Sc2/20 39 . C A 99 Pass N=2 F=5;U=4
**Expected Output:**
Sc2/20 39 . C T 97 Pass N=2 F=5;U=4
Sc2/20 39 . C A 99 Pass N=2 F=5;U=4
Sc2/60 55 . G T 90 Pass N=2 F=5;U=4
Sc2/60 55 . G C 99 Pass N=2 F=5;U=4
Sc2/80 20 . A T 86 Pass N=2 F=5;U=4
Sc2/80 20 . A C 80 Pass N=2 F=5;U=4
Logic:
All the even lines of the input file should be compared and ranked according to the score (Descending Order) and the corresponding odd line of the file should be printed as well with it. If any of the scores (of the even lines) are equal then we need to look at the score of the corresponding odd line and therefore, the higher score gets priority and is printed first.
Namrata
(519 rep)
Aug 7, 2013, 08:45 AM
• Last activity: Mar 5, 2025, 05:31 AM
31
votes
3
answers
2943
views
Why is sort -o useful?
UNIX philosophy says: do one thing and do it well. Make programs that handle text, because that is a universal interface. The `sort` command, at least GNU sort, has an `-o` option to output to a file instead of `stdout`. Why is, say, `sort foobar -o whatever` useful when I could just `sort foobar >...
UNIX philosophy says: do one thing and do it well. Make programs that handle text, because that is a universal interface.
The
sort
command, at least GNU sort, has an -o
option to output to a file instead of stdout
. Why is, say, sort foobar -o whatever
useful when I could just sort foobar > whatever
?
strugee
(15371 rep)
Oct 6, 2013, 04:40 AM
• Last activity: Mar 1, 2025, 11:01 AM
0
votes
2
answers
382
views
How to perform custom sort
I have the following line in a file : [[0, [0, '1']], [1, [0, '2']], [2, [0, '3']], [3, [0, '4']]] [[0, [1, '5']], [1, [1, '6']], [2, [1, '7']], [3, [1, '8']]] [[0, [2, '9']], [1, [2, '10']], [2, [2, '11']], [3, [2, '12']]] Is there a way to sort them on the first character (0,1,2 or 3) in the inner...
I have the following line in a file :
[[0, [0, '1']], [1, [0, '2']], [2, [0, '3']], [3, [0, '4']]]
[[0, [1, '5']], [1, [1, '6']], [2, [1, '7']], [3, [1, '8']]]
[[0, [2, '9']], [1, [2, '10']], [2, [2, '11']], [3, [2, '12']]]
Is there a way to sort them on the first character (0,1,2 or 3) in the inner list element so that I will end up with the following:
[[0, [0, '1']], [0, [1, '5']],[0, [2, '9']]]
[[1, [0, '2']], [1, [1, '6']],[1, [2, '10']]]
and so on?
Andrez
(103 rep)
Dec 3, 2013, 04:10 PM
• Last activity: Mar 1, 2025, 09:55 AM
4
votes
3
answers
7413
views
How to print top five highest numbers from a column?
I have a text file with four columns. I need to read till end of file and print the highest number from column3 (top 5 values) along with column 1. **input.txt** xm|340034177|ref|RT_235820.1| 139697 192 0 xm|161622288|ref|RT_340093.1| 153819 2607 0 xm|75755638|ref|RT_557407.1| 153821 1937 0 xm|10877...
I have a text file with four columns. I need to read till end of file and print the highest number from column3 (top 5 values) along with column 1.
**input.txt**
xm|340034177|ref|RT_235820.1| 139697 192 0
xm|161622288|ref|RT_340093.1| 153819 2607 0
xm|75755638|ref|RT_557407.1| 153821 1937 0
xm|108773031|ref|RT_678101.1| 161452 1688 0
xm|30352011|ref|RT_784766.1| 150568 105 0
**output.txt**
xm|161622288|ref|RT_340093.1| 2607
xm|75755638|ref|RT_557407.1| 1937
xm|108773031|ref|RT_678101.1| 1688
xm|340034177|ref|RT_235820.1| 192
xm|30352011|ref|RT_784766.1| 105
jack
(3933 rep)
Jun 12, 2014, 01:55 AM
• Last activity: Mar 1, 2025, 09:26 AM
5
votes
2
answers
427
views
Issue with "sort -k 1,2" not correctly sorting by first two columns
I have an issue sorting a file based on the first two columns. The layout of the file is: 1 998688068 PizzaFan Insurance 22.47 5 072821325 Plaisio Computers 26.35 4 998688068 PizzaFan Food 27.32 5 456834578 G.Yannopoulos Medical 91.67 .... .... I used this command : `sort -n -k 1,2 "$fpath" -o "$fpa...
I have an issue sorting a file based on the first two columns.
The layout of the file is:
1 998688068 PizzaFan Insurance 22.47
5 072821325 Plaisio Computers 26.35
4 998688068 PizzaFan Food 27.32
5 456834578 G.Yannopoulos Medical 91.67
....
....
I used this command :
sort -n -k 1,2 "$fpath" -o "$fpath.ordered"
The sort result is:
1 473151252 Goodys Food 7.15
1 951515524 Atlantic SuperMarket 41.32
1 998688068 Atlantic SuperMarket 80.23
1 998688068 PizzaFan Food 61.72
1 998688068 PizzaFan Insurance 22.47
2 094321587 Vasilopoulos SuperMarket 6.50
....
....
I don't understand why all columns get sorted (see 3rd column & PizzaFan Insurance)
I think -k 1,2 means sort on column 1 and resolve ties with column 2, but it's like it doesn't work.
It's the same as using the:
sort -n "$fpath" -o "$fpath.ordered"
George
Apr 23, 2011, 07:14 PM
• Last activity: Feb 28, 2025, 04:46 PM
Showing page 1 of 20 total questions