Tenth Line:https://leetcode.com/problems/tenth-line/
题意:打印出一个文件的第10行。
问题涉及到几个点:
1.如何读取一个文件?
2.如何找到第10行?
3.如果文件是空的或者少于10行怎么办?
解法:
1.shell读取文件的一种方法:
while read line do echo $line done < filename2.用计数的方法找到第10行。
3.如果文件是空的或者少于10行,那么输出空。
代码:
cnt=1 while read line do if [ $((cnt)) == 10 ] ; then echo $line fi let cnt+=1 done < file.txt if [ $cnt -lt 10 ];then echo "" fi
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lavorange/article/details/47375491