Tag Info

New answers tagged

4

You seem to be mixing up field positions and character positions. There is no need to split on A; with GNU sort, you can sort on a range of characters within the first space-delimited field: $ sort -k1.4,1.11 -k1.1,1.2 file 01A12345679 12345679 132132155VDVLDLV 02A12345679 FONDSEURO 000012664,120000000M 02A12345679 FR09999956570000009999,999990000F ...


2

Use -g (compare according to general numerical value) to sort: sort --field-separator="A" -g -k2,11 -k1,2 tmp/file.txt > tmp/file_out.txt


0

I try on my gnu/gentoo, no such problem, here it is: ~ # ps -o ppid -o lstart --sort=lstart PPID STARTED 3851 Mon Dec 15 21:25:51 2014 4037 Mon Dec 15 21:25:52 2014 4042 Tue Dec 16 22:02:24 2014 ~ # ps -o ppid -o lstart --sort=lstart PPID STARTED 3851 Mon Dec 15 21:25:51 2014 4037 Mon Dec 15 21:25:52 2014 4042 Tue ...


4

ps --sort=lstart doesn't actually sort by lstart, According to this serverfault comment: lstart gives a full timestamp, but cannot be used as a sort key. start_time gives the usual 'time within the last 24 hours, date otherwise' column, and can be used as a sort key. This is implicitly documented in ps's man pages, where lstart is not listed under ...


2

$ paste prices fruits | sort -k2 | cut -f1 1.01 2.18 4.11 4.52 1.73 1.69 1.09 paste combines the two files, line by line. sort -k2 sorts them on the second column (the fruit name). cut -f1 returns just the first column (the prices). For the above, I assumed that the line numbers shown in the display of the fruits and prices files were an artifact of the ...


3

Depending on the size of the file you can do: sed '/^*/!H;//p;$!d;g;s/\n//' That stacks in Hold space lines which do not match /^*/. Those that do match are printed as they occur in input. Then all lines which are !not the $last are deleted from output. On the last line we get hold space by overwriting pattern space, then the first \newline character is ...


1

Here is a solution using ed (and expanding a bit on the vi solution posted by Kaz, which underneath uses ex - a beefed up ed). Replace w with ,p\n to print the output instead of writing to file. Move the matching lines to the end of the file: ed -s file <<< $'g/^\*/m$\nwq\n' The global subcommand marks every line that matches the pattern. ...


1

You can use: tr -dc '\-0-9\n' | sort -u -t- -nk1,1 | grep -c . ...which is, admittedly, more than a little inspired by muru's answer here. Differently, though, I use grep to count the lines rather than wc in case there are blank lines in input. His answer doesn't have a blank line problem as grep -o will only print lines with their match (as grep -c only ...


0

An awk solution awk -F'-' '{sub(/[^[:digit:]]+/, "", $1); a[$1]} END{for (k in a) ++i; print i}' file 8


1

Use extended grep to and look for four digits, telling grep to only list the matches (as opposed to the whole line, which is the default): grep -Eo '[0-9]+' <filename> Sort this list of numbers and only output unique ones: sort -u Count the number of lines: wc -l Put it all together: $ grep -Eo '[0-9]+' filename | sort -u | wc -l 8


4

With grep, filter out just the numbers: grep -Eo '[0-9]+-' file | sort -u | wc -l [0-9] Matches any character between 0 and 9 (any digit). + in extended regular expressions stands for at least one character (that's why the -E option is used with grep). So [0-9]+- matches one or more digits, followed by -. -o only prints the part that matched your ...


0

With perl, without any shell pipes (quicker) : $ perl -lne '/\d+-/ and $h{$&}++;END{print scalar keys %h}' file


2

GNU sort and xargs might do the trick printf '%s\0' ullman*.pbm | sort -z -k2,2n -t'-' | xargs -0 convert First check this works by listing files without calling convert printf '%s\0' ullman*.pbm | sort -z -k2,2n -t'-' | xargs -0 printf '%s\n' ullman-000.pbm ullman-001.pbm ullman-098.pbm ullman-099.pbm ullman-100.pbm ullman-1000.pbm ... Whereas ...


1

One way would be to rename the files that have three digit numbers to four digit ones, padded with a zero. If you have perl-rename (installed by default on Ubuntu) you can try: rename -n 's/-(\d{3}\.)/-0$1/' *.pbm Once you're satisfied with the result, run again without the -n. Or see other options in Padding a number in a filename to a fixed length.


1

Here's one way: Make the log files NUL-delimited. That is, make every record end with a NUL (\0) character. Then you can avail of the support for NUL-delimited text found in a number of tools (sed, sort, xargs, find, etc.). One way could be to do: perl -pe 's/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/\0$1/' file.log.2 > file.log.2.NULL Prepend every ...


5

You can just pass through sort: $ md5deep -rl * | sort -k2 d41d8cd98f00b204e9800998ecf8427e 2014-12-01/IMG_1969.png bd12c358db0c97230b9d48f67b2c0c98 2014-12-01/IMG_1970.png c3a9d8cb047192a03b857023948a7ba6 2014-12-01/IMG_1971.png If your file name can contain newlines or other strangeness, use this instead (assumes GNU sort): $ md5deep -0rl * | sort -zk2 ...


0

I think a big part of the problem here is ... -k 1 ... sort's -key arguments imply at least a -k [num] start and ,[num] end reference for each. To say -k 1 is effectively no different than specifying no sort key at all because without an end reference sort will sort lines from the start of the line to its end. If you wanted to sort lines on only the ...


1

First, read the header from one of the files. Then read the data from both and sort: head -n 1 sample1.csv > results.csv && tail -q -n +2 sample1.csv sample2.csv | sort -t "|" -k 1 >> results.csv


5

This should work and output to data2.csv: head -n 1 data1.csv > data2.csv && tail -n +2 data1.csv | sort -t "|" -k 1 >> data2.csv


2

| sort | uniq -c As stated in the comments. Piping the output into sort, organises the output into alphabetical/numerical order. This is a requirement because uniq only matches on repeated lines, ie a b a If you use uniq on this text file, it will return the following. This is because the a is separated by the b - they are not repeating lines. a b a ...


-1

I'm pretty sure you're looking for join. Unfortunately, I'm not very good with it. I know there's a way to make it fill the fields the way you want as well, but so far I can only get the unpaired lines to print at the head of the line. join only joins two files at a time, and so the unpaired lines don't show where you want - at least, I think they don't. ...


0

What you want to do requires a little programming: #!/usr/bin/perl # Program to join files of TAB separated data based on first key # --J. Ziobro--: 11/2014 use strict; my $f; my %allLines; my $maxColumns = 0; my $fileNum = 0; my %keys; foreach $f (@ARGV) { die "Could not open $f" unless open( F, $f ); while (<F>) { chop; my ...


15

Izkata's comment revealed the answer: locale-specific comparisons. The sort command uses the locale indicated by the environment, whereas Python defaults to a byte order comparison. Comparing UTF-8 strings is harder than comparing byte strings. $ time (LC_ALL=C sort <numbers.txt >s2.txt) real 0m5.485s user 0m14.028s sys 0m0.404s How about ...


7

This is more of an extra analysis than an actual answer but it does seem to vary depending on the data being sorted. First, a base reading: $ printf "%s\n" {1..1000000} > numbers.txt $ time python sort.py <numbers.txt >s1.txt real 0m0.521s user 0m0.216s sys 0m0.100s $ time sort <numbers.txt >s2.txt real 0m3.708s user ...


5

Both of the implementations are in C, so a level playing field there. Coreutils sort apparently uses the mergesort algorithm. Mergesort does a fixed number of comparisons which increases logarithmically to the input size, i.e. big O(n log n). Python's sort uses a unique hybrid merge/insertion sort, timsort, which will do a variable number of comparisons ...


0

This one handles filenames with whitespace or apostrophes, and works on systems which do not support xargs -d or sort -h: du -s * | sort -n | cut -f2 | tr '\n' '\0' | xargs -0 -I {} du -sh "{}" which results in: 368K diskmanagementd 392K racoon 468K coreaudiod 472K securityd 660K sshd 3.6M php-fpm


5

sort -V input from man sort: -V, --version-sort natural sort of (version) numbers within text that will get you: CPL_V11.01.00_1 CPL_V11.01.00_2 CPL_V11.01.00_3 CPL_V11.01.00_4 CPL_V11.01.00_10 CPL_V11.01.00_35 CPL_V11.01.00_36 CPL_V11.01.00_37 CPL_V11.01.00_38 CPL_V11.01.00_39 CPL_V11.01.00_40 CPL_V11.01.00_41 CPL_V11.01.00_42 ...


2

You need to tell sort -n to sort on the part after the =: sort -t= -k2n


-1

use sort: sort -n file1 > file2 -n, --numeric-sort compare according to string numerical value sort -g file1 > file 2 -g, --general-numeric-sort



Top 50 recent answers are included