【leetcode-shell】Tenth Line

来源:互联网 发布:实体关系图软件 编辑:程序博客网 时间:2024/06/05 16:08


随便找了一个shell脚本的题目。


打印出第十行的内容。


解法一:循环,不过效率最差,排在了最后

# Read from the file file.txt and output the tenth line to stdout.#!/bin/bashdeclare count=0cat file.txt | while read linedo    if [ $count = 9 ];then        echo $line        exit 0    fi    let count=$count+1done

解法二:sed,最快速,也最简洁

# Read from the file file.txt and output the tenth line to stdout.#!/bin/bashsed -n 10p file.txt


原创粉丝点击