Q
最短线的长度?
6
A
回答
11
试试这个:
awk '{print length}' <your_file> | sort -n | head -n1
此命令获取长度的所有文件,对它们进行分类(正常,如数字)和,fianlly,打印到控制台的最小数目。
10
纯awk的解决方案:
awk '(NR==1||length<shortest){shortest=length} END {print shortest}' file
+0
这似乎稍快。 – ADTC
0
我把awk命令到一个函数(对于bash):
function shortest() { awk '(NR==1||length<shortest){shortest=length} END {print shortest}' $1 ;} ## report the length of the shortest line in a file
将此添加到我的.bashrc(然后是“源的.bashrc “)
然后运行它:最短的”yourFileNameHere“
[~]$ shortest .history 2
它可被分配给一个变量(注意所需的backtics):
[~]$ var1=`shortest .history` [~]$ echo $var1 2
对于csh:
alias shortest "awk '(NR==1||length<shortest){shortest=length} END {print shortest}' \!:1 "
0
从上方不处理这两种awk
解决方案 '\ R' wc -L
的方式。 对于单行输入文件,它们不应产生大于wc -L
报告的最大行长度的值。
这是一个新sed
基于溶液(I无法同时保持正确的缩短):
echo $((`sed 'y/\r/\n/' file|sed 's/./#/g'|sort|head -1|wc --bytes`-1))
这里有一些样品中,示出了 '\ R' 如权利要求并证明sed
溶液:
$ echo -ne "\rABC\r\n" > file
$ wc -L file
3 file
$ awk '{print length}' file|sort -n|head -n1
5
$ awk '(NR==1||length<shortest){shortest=length} END {print shortest}' file
5
$ echo $((`sed 'y/\r/\n/' file|sed 's/./#/g'|sort|head -1|wc --bytes`-1))
0
$
$ echo -ne "\r\r\n" > file
$ wc -L file
0 file
$ echo $((`sed 'y/\r/\n/' file|sed 's/./#/g'|sort|head -1|wc --bytes`-1))
0
$
$ echo -ne "ABC\nD\nEF\n" > file
$ echo $((`sed 'y/\r/\n/' file|sed 's/./#/g'|sort|head -1|wc --bytes`-1))
1
$
相关问题
- 1. 最短长度子集
- 2. 消息的最短可预期长度
- 3. MySQL的最短长度匹配
- 4. Google短网址 - 最大网址长度
- 5. 短信队列最大长度?
- 6. 在有向图中找到正长度的最短长度的循环
- 7. HTTP起始线的最大长度
- 8. Hadoop流最大线路长度
- 9. Woocommerce简短摘要长度
- 10. 如何缩短url长度?
- 11. 不能缩短NSString长度
- 12. 线长度
- 13. 折线长度
- 14. 的OpenCV houghlines2线路长度是办法短
- 15. 最短方法查找单词的最小和最大长度阵列
- 16. 在迷宫中打印最短路的长度
- 17. 使用map/reduce找到最短的唯一前缀长度
- 18. 截断data.frames在列表中的最短长度
- 19. 无向图中最短周期的长度
- 20. 计算加权最短路径的未加权长度
- 21. 迭代所有最短长度字符串的算法?
- 22. 找到感兴趣区域之间的最短长度
- 23. 从python列表中获取最短长度的listitem
- 24. Cuboid中的Ant找到最短路径:给出长方体的长度,宽度和高度。输出应显示浮点的最短距离
- 25. 最大长度
- 26. 最长最短路径(不完全)
- 27. 创建曲线给定的最小值,最大值,和长度
- 28. 如何在matplotlib轮廓图中设置短划线长度
- 29. d3.js x轴的长度太短
- 30. 如何缩短输入的长度
http://superuser.com/q/135753/109661 – hovanessyan