is there efficient way sort file while ignoring string appears @ beginning of lines?
for example, have list of files this:
fileaardvark filebee n-filebear n-filecat filezebra and want sort while ignoring "n-", sorted result be
fileaardvark n-filebear filebee n-filecat filezebra i need not edit lines directly, otherwise strip 'n-' file entirely. initial thought use 'n-' delimiter sort, fails both because 'n-' multiple characters , because column number need differs line line.
in particular case, happened strings started same term (i.e., in example, has "file" @ beginning) , needed sort on entirety of remaining line, ended using sed/sort chain, got me result wanted:
sed -e 's/file/\x06/g' | sort -t$'\x06' -k2 | sed -e 's/\x06/file/g'
however, can't depend on having duplicated sequence @ beginning of strings relevant sorting, how can accomplish in more general way?
the easy approach prepend field n- stripped front of line, sort stream, strip prefix off.
with gnu sed (having -r; macos or modern bsd sed substitute -e):
sed -r -e 's/^((n-)?([^ ]*))/\3 \1/' <<<"$str" | sort | sed -r -e 's/[^ ]+ //' ...this similar preferred approach sorting files modification time -- places modification time before each name in nul-delimited stream (since nul character cannot exist in file's pathname), sorts initial field, strips off.
Comments
Post a Comment