简体   繁体   English

排序写入失败的标准输出中断管道--Linux

[英]sort write failed standard output broken pipe--Linux

I am using following commands in my script: 我在脚本中使用以下命令:

max_length=`awk '{print length}' $File_Path_Name/$filnm | sort -nr | head -1`;
min_length=`awk '{print length}' $File_Path_Name/$filnm | sort -nr | tail -1`;

where the filenm variable contains the name of the file and File_Path_Name contains the directory path. 其中filenm变量包含文件名, File_Path_Name包含目录路径。
While executing this from script I am getting the error 从脚本执行此操作时出现错误

sort: write failed: standard output: Broken pipe 排序:写入失败:标准输出:管道损坏

Any suggestions what I am doing wrong? 有什么建议我做错了吗?

you don't need to scan the file twice for getting max/min try 您无需两次扫描文件即可获得最大/最小尝试次数

$ read max min < <(awk '{print length}' file | sort -nr | sed -n '1p;$p' | paste -s)

or you can avoid sorting as well by calculating max/min within awk 或者您也可以通过计算awk内的最大值/最小值来避免排序

$ awk    '{len=length} 
   NR==1  {max=min=len} 
   max<len{max=len} 
   min>len{min=len} 
   END    {print max, min}' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM