sorting - 如何使用 awk 按长度对行进行排序?

标签 sorting awk

我有以下 Python 脚本,用于按长度对行进行排序:

import fileinput
print "".join(sorted(fileinput.input(), key=len))

如何在 Awk 中编写相同的脚本?

最佳答案

使用awksort的几个解决方案:

# 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/

相关文章:

perl - 如何在 dd :mm:yyyy hh24:mi:ss format in descending order in Perl? 中对时间戳进行排序

algorithm - 使用 perl 按依赖项排序数组

sed - 删除包含另一个图案的多线图案

bash - 搜索和追加

linux - 从命令行将多个文件中的列提取到单个输出文件中

regex - 如何使用 awk 匹配多个变量模式?

java - 如何根据值以升序打印 HashMap<String, String> 的内容?

c++ - 如何用随机数填充文件?

c - 是否可以使用预处理器对数组进行排序?

perl - 如何跳过第一次出现的模式并替换第二次出现的字符串的其余部分