I am listing directories and I would like to sort by name, but ignore leading "The", ie the output should be:
Apple
Th开发者_Python百科e Banana
Orange
and not
Apple
Orange
The Banana
What is the best way to do it?
In fact I'm gonna put that in writing.
My suspicion is that the following will do what you want it to do:
ls -d * | sed 's/^The /\t/' | sort -b | sed 's/^\t/The /'
I'll explain. ls -d *
lists all the directories. sed -r 's/^The /\t/'
replaces all leading "The "s with tabs (a whitespace placeholder), sort -b
sorts, ignoring leading whitespace (the tab), and sed 's/^\t/The /'
replaces all those leading tabs with "The " again.
ls | sed -e 's/^The \(.*\)/\1, The/' | sort | sed -e 's/\(.*\), The$/The \1/'
This solution works, but is silly and won't work in every case (eg there are double spaces)
find . -type d | grep -o "[^./].*" | sed 's/^The/ /' | sort | sed 's/ /The /'
精彩评论