Tag Info

New answers tagged


Since the Schwartzian transform has been mentioned, I'm surprised to see that no-one has yet posted a pure Perl implementation of one: perl -ne 'push @a, $_ }{ print map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [$_, $_ =~ tr/a//] } @a' file a man a plan a canal panama aardvark baseball cat bat bill Each line of the file is pushed to ...


Another Schwartzian transform: $ awk -Fa '{print NF,$0}' file | sort -nr | cut -d' ' -f2- a man a plan a canal panama aardvark baseball cat bat bill Or, in Perl: perl -Fa -lane 'print "$#F $_"' file | sort -nr | cut -d' ' -f2-


You can also just sort on the character: tr -cd a\\n <file | paste - ./file | LC_ALL=C sort -rk1,1 | cut -f2- Here's what your example looks like after being translated and pasted before it is piped into sort: aa baseball aaa aardvark aaaaaaaaaa a man a plan a canal panama a cat a bat bill Then sort gets it and, all things being equal, ...


The general approach with this kind of task is to use awk or perl... to compute the metric you're interested in and prepend it to the line, and then feed that to sort and remove the metric off the sorted output: awk '{print gsub("a","a"), $0}' < file | sort -rn | cut -d' ' -f2-


#!/bin/bash cat input.txt | while IFS= read -r a; do b=${a//[^a]} echo "${#b} $a" done | sort -rn | sed 's/[^ ]* //'


I am grateful for the code user78605 has provided, since it guided me in the direction of how to find the median in my queries. The above-mentioned code, however, overlooks certain conditions which are required to calculate the median correctly. Issues: Blank trailing lines (if they exist in the file), should not be counted since counting them affects the ...


sort -t"," -k 2 file 2854.11,18:f6:43:64:81:67 3540.68,18:f6:43:64:81:67 1616.93,ac:22:0b:a6:22:c3 3856.91,ac:22:0b:a6:22:c3 2872.32,c0:bd:d1:36:bb:49 2497.93,d4:0b:1a:39:19:b2 3314.55,d4:0b:1a:39:19:b2 Just needed to use -t"," as the delimiter. And file stores your unsorted data.


Try this. Sets the delimiter to =, and then uses field 2 from character 2 onwards (ignoring the "("). sort -t= -k 2.2n file.txt fn=(916) __universe_MOD_general_main fn=(1368) __universe_MOD_general_boot cfn=(1370) __lib_file_MOD_file_open fn=(6142) __grid_var_MOD_get_overlap Or even sort -t\( -k 2n <foo fn=(916) __universe_MOD_general_main fn=(1368) ...


How about: sort -nt'(' -k2 file.txt Test : $ sort -nt'(' -k2 file.txt fn=(916) __universe_MOD_general_main fn=(1368) __universe_MOD_general_boot cfn=(1370) __lib_file_MOD_file_open fn=(6142) __grid_var_MOD_get_overlap -n indicates we are sorting numerically t'(' sets the delimiter as ( -k2 sets the key to sort as the second field i.e. starting from ...


grep tcp *.tcp | grep open | sort -u Giving multiple filenames to grep will, by default, cause grep to prefix the matching output lines with the filename(s) they matched in. The only other change I made was to combine sort | uniq into sort -u (and to remove the quotes that are unnecessary here).


grep -H 'tcp' -H, --with-filename Print the file name for each match. This is the default when there is more than one file to search.


To do this with ls robustly you should not split on anything but the path delimiter - that's what it's for. IFS=/; set -f set -- $(ls -dt ./*) shift That will sort all non-dot files in the current directory and place the results in the shell array $@. Given a POSIX ls, this is not susceptible to any kind of filename mangling whatsoever: special characters ...


In zsh it's as easy as array=(*.sh(Nom)) The glob qualifier om causes the matches to be sorted by modification time (newest first), and N forces the array to be empty if there is no match (instead of causing an error). In other shells such as bash, there's no good way of sorting by time. You can use ls -t, but that can break because the output is ...

Top 50 recent answers are included