开发者

unix command - ignore "The" while sorting

开发者 https://www.devze.com 2023-01-28 07:34 出处:网络
I am listing directories and I would like to sort by name, but ignore leading \"The\", ie the output should be:

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 /'
0

精彩评论

暂无评论...
验证码 换一张
取 消