Hot answers tagged

11

sort -t '|' -k5,5 Using | as the field separator, this sorts by the 5th column only.


5

sort -nt '_' -k2 Output: file_0 file_1 file_2 file_3 file_4 file_5 file_6 file_7 file_8 file_9 file_10 file_11 file_12 file_13 file_14 file_15 or: file_0.txt file_1.txt file_2.txt file_3.txt file_4.txt file_5.txt file_6.txt file_7.txt file_8.txt file_9.txt file_10.txt file_11.txt file_12.txt file_13.txt file_14.txt file_15.txt Tested with FreeBSD and ...


4

You can add an awk line to the end of your command. For example, sort words.txt | uniq -c | awk '{print $2","$1}' Basically, it takes the second column and places it before the first column while separating it by a comma. I do not know how expensive it is to run it on a 30-40Gb file.


3

Using sxiv: If your file names don’t contain newlines, it’s easy. This works because sxiv - accepts stdin: ls -Sd -- *.png | sxiv - This GNU variant uses a NUL terminated list and allows you to use the power of find. If you want recursion, remove -maxdepth 1: find . -maxdepth 1 -name '*.png' -printf '%s\t%f\0' | sort -znr | cut -z -f'2-' | xargs -r0 sxiv ...


3

sort file | awk '{print > substr($0, 3, 1) ".txt"}' Will print each line into a file whose name is the third character on the line followed by .txt.


2

Please try this: output | awk '{print gensub("[^0-9]*","","g") " " $0 }' | sort -n | awk '{$1=""; print $0}' | sed 's/^ //g' This isn't the most elegant solution but it works.


2

try: printf '%s\n' "${IPS[@]}" |sort |uniq -c |sort -rn |sed 's/^ *//' 3 1.1.1.1 2 5.5.5.5 1 3.3.3.3 1 2.2.2.2 related: Printing an array to a file with each element of the array in a new line in bash Why is printf better than echo?


2

You haven't defined "sort correctly" anywhere, so I'm going to assume that you want to group by the first column and order by ascending numerical value of the second, with duplicate values removed. This solution isn't what you've actually asked for, but it seems to be what you want. sort -k1,1 -k2,2n -u datafile female 4 female 13 male 1 male 9 male 11 male ...


1

$ sort -r file | awk 'index(prev,$0) != 1; {prev=$0}' h ii j dddd eeee fe dddd eeee f g aaaa bbbb cc aaaa bbbb c dd If the output order matters to you there's ways to address that, e.g.: $ cat -n file | sort -k2r | awk '{orig=$0; $1=""} index(prev,$0) != 1{print orig} {prev=$0}' | sort -n | cut -f2- aaaa bbbb c ...


1

The sort specification describes this in a little more detail: The 'b' modifier shall behave like the -b option, but shall apply only to the field_start or field_end to which it is attached. The other modifiers shall behave like the corresponding options, but shall apply only to the key field to which they are attached; they shall have this effect if ...


1

You can use an associative array to store the different IPS as keys which will increment when iterating over the IPS array. #!/bin/bash IPS=("1.1.1.1" "5.5.5.5" "3.3.3.3" "1.1.1.1" "2.2.2.2" "5.5.5.5" "1.1.1.1") declare -A arr for ip in ${IPS[@]}; do ((arr[${ip}]++)) done for k in ${!arr[@]}; do echo "${arr[$k]} $k" done | sort -rn


1

perl -0ne 'print join "\\", sort split(/^\\/m)' input.txt This is based on the Sort file by group of lines answer linked to by @αғsнιη. It splits the entire input file into an array, using the regexp ^\ as the delimiter. Then it prints the sorted array, with the array elements joined by a single \ character. The join is necessary, because it puts back ...


1

With the zsh shell and any image viewer that will display the images in the order they are given on the command line, you can use the oL glob qualifier to sort a glob expansion by size: feh ./*.jpg(oL) # from smallest to largest feh ./*.jpg(OL) # from largest to smallest feh ./*.jpg(^oL) # from largest to smallest


1

The answer(one of them, I'm sure) is: sort -t _ -k 2 -g [filename with names+numbers or piped from another command with | - both situations will work ]


1

With sed: sort words.txt | uniq -c | sed -E 's/^ *([0-9]) (.+)/\2,\1/g' Tested with GNU, Busybox and BSD implementations of sed. Output would be: america,2 and,4 england,1 file,1 for,1 place,1 I ran a test on 200MB file and noticed that sed itself is still quite fast: $ time sed -E 's/^ *([0-9]) (.+)/\2,\1/g' HUGE | head america,2 and,4 england,1 ...


1

we can do with awk itself... Try below, awk '{j[$0]++} END {for (i in j) print i","j[i]}' words.txt


Only top voted, non community-wiki answers of a minimum length are eligible