I hav开发者_运维问答e a text document (1MB, TXT file) with a little more than 17,500 lines. What I'm hoping to be able to do is to sort those lines by character length and have it output to either the same file (which is then saved) or a new file entirely. Either one works fine as long as I know ahead of time.
Bonus points if I could do it through Automator in OS X in some way as my coding/terminal abilities are... Lacking.
I converted the file to XML then used XSLT to order the entries based on string length. It was a really long way around but it worked.
awk '{printf "%7d %s\n", length($0), $0}' file | sort -n | sed -e 's/^....... //' > newfile
print each line with its length before it in a 8 character field sort that output numerically strip off the 8 characters from the front of each line
This works if each line of your file has fewer than 10M characters. Since your file is less than 1MB that must be true.
精彩评论