码迷,mamicode.com
首页 > 系统相关 > 详细

【leetcode~Shell】:Tenth Line

时间:2015-08-09 17:18:27      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:shell   linux   leetcode   

Tenth Line:https://leetcode.com/problems/tenth-line/


题意:打印出一个文件的第10行。

问题涉及到几个点:

1.如何读取一个文件?

2.如何找到第10行?

3.如果文件是空的或者少于10行怎么办?

解法:

1.shell读取文件的一种方法:

while read line
do 
    echo $line
done < filename
2.用计数的方法找到第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



版权声明:本文为博主原创文章,未经博主允许不得转载。

【leetcode~Shell】:Tenth Line

标签:shell   linux   leetcode   

原文地址:http://blog.csdn.net/lavorange/article/details/47375491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!