我有以下 Python 脚本,用于按长度对行进行排序:
import fileinput
print "".join(sorted(fileinput.input(), key=len))
如何在 Awk 中编写相同的脚本?
最佳答案
使用awk
和sort
的几个解决方案:
# keep spaces
awk 'OFS = "\t" { print length, $0 }' file | sort -g | cut -f2-
# stripping out spaces from line before counting
awk 'OFS = "\t" { gsub (" ", "", $0); print length, $0 }' file | sort -g | cut -f2-
关于sorting - 如何使用 awk 按长度对行进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401683/