Tag Info

New answers tagged

0

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 ...


1

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 ...


0

An array might not be necessary: for f in $(ls -t *.sh); do echo "$f" done If an array is necessary: files=( $(ls -t *.sh) ) Doing things with the array: # Echo the number of elements in the array: echo "${#files[@]}" # Echo the values of each array element: echo "${files[@]}" # Echo just the first array element: echo "${files[0]}" # Assign the ...


1

(I like the paste/cut answer of @taliezin. Here's a more prosaic solution in case of many columns). Split the data into one file per column, sort these files, and merge back the files. I'm assuming space separates the columns. Also, what to do when column 2 is shorter than column 3? I've just used fixed-width columns for this demo, where data0 is the initial ...


1

You can try something like this: paste -d'\t' <(cut -f 1 -d' ' file | sort -u) <(cut -f 2 -d' ' file | sort -u) <(cut -f 3 -d' ' file | sort -u) <(cut -f 4 -d' ' file | sort -u) >output I put tab as delimiter of paste to be more visible the output.


1

export LC_COLLATE=C export LANG=C cat big_file | sort > /dev/null Usally Linux sort does some nifty stuff to comply to Unicode equality rules... if you change the locale to C it switches to byte only... For a 1.4GB file the difference on my machine is 20s vs. 400s (!!!)


0

By executing this: LC_COLLATE=C sort -k1,1 -k2,2n -k3,3n -k4,4n file I got the exact output that you wanted: chr1 17555949 17555950 1 chr1 101466461 101466462 1 chr1 161407811 161407812 1 chr1 202930379 202930380 1 chr11 118560806 118560807 1 chr12 8121782 8121783 1 chr12 49449119 49449120 1 chr12 77244463 ...


0

sed is the more simple and efficient tool for this editor grade modification: sed 's-,-ctrlVtab-g' customers You can also very easily achieve the same modification with ex, vi or emacs.


3

Using the GNU implementation of sort sort -V foo from man sort on a GNU system: -V, --version-sort natural sort of (version) numbers within text Example cat foo chr1 101466461 101466462 1 chr6 160143888 160143889 1 chr19 19231081 19231082 1 chr18 47008735 47008736 1 chr1 161407811 161407812 1 chr4 ...


1

TXR Lisp step by step: $ cat data Echo Alpha Delta Charlie Golf Bravo Hotel Foxtrot $ txr -p '(get-lines)' < data ("Echo" "Alpha" "Delta" "Charlie" "" "Golf" "Bravo" "Hotel" "Foxtrot") $ txr -t '(get-lines)' < data Echo Alpha Delta Charlie Golf Bravo Hotel Foxtrot $ txr -p '(partition* (get-lines) (op where [chain length zerop]))' < data ...


1

I wrote a tool in haskell that allows you to use sort, shuf, tac or any other command on paragraphs of text. https://gist.github.com/siers/01306a361c22f2de0122 It splits the text into blocks, joins the subblocks with \0 char, pipes through the command and finally does the same thing in reverse.


2

The one thing that always helps me most with sort is giving it as much memory as possible, so as to reduce swapping, e.g.: sort -S 20G


0

I'd use perl and a hash. Something like: #!/usr/bin/perl use strict; use warnings; my %seen; while ( <> ) { print unless $seen{$_}++; } I think this'd one-liner-ify as: perl -ne 'print unless $seen{$_}++' data.txt (Or cat data into it). This works on getting unique whole lines - you can also use split or regular expressions ...


2

To make sure I added a new line in the example data: exams2008/Zoology/Summer/ZY4019Sum08.pdf exams2009/Zoology/Summer/ZY4019.pdf exams2010/Zoology/Summer/ZY4019Sum2010.pdf exams2011/Zoology/Summer/ZY4019Sum2011.pdf exams2012/Zoology/Autumn/ZY4019Aut2012.pdf exams2012/Zoology/Summer/ZY4019Sum2012.pdf exams2013/Zoology/Autumn/ZY4019Aut2013.pdf ...



Top 50 recent answers are included