Tag Info

New answers tagged

2

Without the closing field number with -k, it will consider all fields starting from 1. You need : sort -k1,1 -t'|' file.txt Test : $ sort -k1,1 -t'|' file.txt axis-AXIS|OK axis-AXIS1RX|OK axis-AXISA|OK axis-AXISD|OK axis-AXISD1|OK axis-AXISD2|FALSE axis-AXISD2X|OK axis-AXISE|OK axis-AXISM|FALSE axis-AXISMD|FALSE axis-AXISR|OK axis-AXISR1M|OK If your ...


3

You are asking the wrong question, or asking the question wrongly and in the wrong stack, this is a better question to ask in the programming/stack-overflow for people to give you answers based on the algorithms used inside awk and sort. PS: also do the needed with nawk, mawk and gawk to give us some more details to "zone into" ;) and do the runs like a 100 ...


0

Late answer, but it might help someone. If you have a recent version of GNU sort (from GNU coreutils 7.0 or later), you can use the --version-sort (or -V) option, which will do the right thing with IPv4 addresses. Assuming input of: add method1 10.1.2.3 other thing add method2 10.10.20.30 other thing2 add method2 10.1.2.3 other thing2 add method5 10.2.8.9 ...


2

This script copies the ip address from field 3 using awk to the start of the line with a "%" separator, then does the sort on the ip address now in the first field, then removes the added part. awk '{print $3 " % " $0}' | sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 | sed 's/[^%]*% //' If the field with the ip address is not a constant, you can auto-detect it on ...


0

This is expected, make sure you understand the -t option (man sort: field separator). The command you wanted to use deals with plain ip addresses only. A quick and dirty solution might be to convert the spaces into dots . the file first and then sort (you may want to undo the conversion later, excluding the IP addresses) sed -i.bak 's/ /./g' data.log ...


-1

The speed difference is because 'sort' is a command (link), whereas 'awk' is a programming language (link). 'sort' command is takes input and return output. Whereas 'awk' is a programming language, which first interprets the code (terminal command) then starts processing on it. Simple as that.


1

I've found that sort seems to be the fastest uniq tool as shown here --> Fastest way to delete duplicates in large wordlist


5

Perl one-liner: print sort { split(' ',$a) <=> split(' ',$b) } <>; If you want to break ties using alphabetical order: print sort { split(' ',$a) <=> split(' ',$b) or $a cmp $b } <>;


4

Through python. s = '''hello: world foo bar baz bar: baz: bin boop bop fiz bang beep bap: bim bam bop'''.splitlines() for i in sorted(s, key=lambda x: len(x.split()), reverse=True): print(i) or with open('/path/to/the/input/file') as f: m = f.readlines() for i in sorted(m, key=lambda x: len(x.split()), reverse=True): print(i, end="") ...


6

In recent GNU awk one can use PROCINFO array to define many internal parameters including order in which array elements are printed (controlled by element "sorted_in"). Thus we can built and array indexed with the value of NF" "NR, which elements have value of $0 and print it in desired output, in your case that would be "@ind_num_desc": awk '{a[NF" ...


16

You could do something like: awk '{print NF,$0}' file | sort -nr | cut -d' ' -f 2- Basically use awk to print the number of fields then sort by that number an then remove the number. You could do all that in awk (or perl etc) but you get the idea.


2

As St├ęphane Chazelas said in the comment, it is a bug in the specific implementation of coreutils (in coreutils-8.22-11.el7) by CentOS/Red Hat, more specifically in the buggy internationalisation patch (coreutils-i18n.patch) they wrote and applied on top of GNU's coreutils-8.22. I reported it here to CentOS and also here to Red Hat. It was already known at ...


0

Your default collation(en_US.UTF-8) cause to this. You should set LC_COLLATE value to order the text as directed. LC_COLLATE='C' sort -k1 file1


1

Within Red Hat Enterprise Linux 5, no. Unless Red Hat releases an enhancement to the package that provides the sort binary, the "human readable sort" behavior will never appear in any RHEL 5 point release. You may be able to install a RHEL 6 package to get this behavior, but I strongly recommend against doing so.


0

There's a bit of a technical problem here - in that numeric sorting looks at numbers, but alphabetic sorting has 1, 10, 2, 3 etc... I'd suggest - whip out the perl as you can define a custom sort algorithm. Your sort function is handed $a and $b - and is expected to return -1, 0 or +1 depending on their relative positions. perl has two built in operators ...


4

You could do it with a while loop and $RANDOM like: while read line; do if ((RANDOM%2)); then echo $line; fi; done < _path_ which will print about half the lines, which you could control with the condition in the if


13

Use this: nl file | shuf -n2 | sort -n | cut -f2- nl to number the lines, shuf to shuffle and limit the output to 2 lines (-n), sort to rebuild the original order, and cut to remove the numeration of nl. It will print 2 lines of your file in the original order of the file. Use shuf -n X, where X can be any number.


5

The selection of a random line from a file without sorting (or even knowing how many lines there are!) is covered in "The Art of Computer Programming", Volume 2, Section 3.4.2 by Donald E. Knuth. This is trivial to implement, e.g.: (echo foo; echo bar; echo zot) \ | perl -nle 'rand $. < 1 && ( $line = $_ ); END { print $line }' Or try shuf, ...


0

The sort command allows to use the null as tabulation character via -t \0 (as well as the -z proposed by Arcege). Therefore: find folder1 folder2 -name "*.txt" -print0 | sort -t \0 | xargs -0 myCommand Using \n as separator produces a better readable intermediate outputs, but as stated by Ole Tangue reply, it will make it harder to handle with xargs. ...


1

Using the same ls -CFUd as muru but in zsh, you could try with: setopt nullglob ls --color -CFUd -- *(/) *(*) *(@) *(p) *(=) *(^/*@p=) where (...) are glob-qualifiers matching directories, executables, symlinks, pipes, sockets and respectively everything else.


1

If dirty hacks are welcomed, the following might come close: ls -C --color -F -1 | rev | sort | rev Essentially: rev to get the last character first then sort, which will now use the last character first then rev again to get back the original line This, unfortunately, has single-column output. You can apply column to it to get multi-column output, ...


0

With zsh: setopt extendedglob print -rl -- **/file.php(.om:h) .om sorts the selected files by mtime and :h removes the trailing path component. If you want to list mtimes too, you could use zstat: zmodload zsh/stat for f (**/file.php(.om)) printf '%s %s\t%s\n' $(zstat -F '%d-%b-%Y %T' +mtime -- $f) ${f:h}



Top 50 recent answers are included