读文件并返回其中最大最小行的shell脚本

来源:互联网 发布:java设计模式pdf下载 编辑:程序博客网 时间:2024/05/29 12:51

http://www.linuxsir.org/bbs/thread360346.html

我想实现这样的操作.
对一个文件做统计,统计出文件里的最大和最小所在行.

文件描述:
a0003b
c0002d
f0005d
f0006v
a0004t
......
......
这文件每列2到5的字符是有序的数值,其他字符无序

预期结果是返回最大值所在的记录行:f0006v, 最小值所在的记录行c0002d

 

相匹配的

Shell代码 复制代码
  1. root [ ~/fnz ]0# grep `sed -e 's/^.//' data | sort | head -n 1` data   
  2. c0002d   
  3. root [ ~/fnz ]0# grep `sed -e 's/^.//' data | sort | tail -n 1` data   
  4. f0006v   
  5. root [ ~/fnz ]0#  
root [ ~/fnz ]0# grep `sed -e 's/^.//' data | sort | head -n 1` datac0002droot [ ~/fnz ]0# grep `sed -e 's/^.//' data | sort | tail -n 1` dataf0006vroot [ ~/fnz ]0#


 

Shell代码 复制代码
  1. root [ ~/fnz ]0# sort -k1.2,5 data | sed -n '1p;$p'  
  2. c0002d   
  3. f0006v   
  4. root [ ~/fnz ]0#  
root [ ~/fnz ]0# sort -k1.2,5 data | sed -n '1p;$p'c0002df0006vroot [ ~/fnz ]0#