您的位置:首页 > 运维架构 > Shell

Tenth Line

2016-01-05 14:17 567 查看
题目大意:给一个多行的文件,让你写一段shell脚本把文件的第10行输出来。

方法一:

# Read from the file file.txt and output the tenth line to stdout.
count=0
while read line && [ $count -le 10 ]
do
count=$[$count+1]
if [ $count -eq 10 ]
then
echo $line
exit
fi
done < file.txt

方法二:

# Read from the file file.txt and output the tenth line to stdout.
sed -n '10p' file.txt

方法三:

# Read from the file file.txt and output the tenth line to stdout.
awk '{if(NR==10) print $0}' file.txt


方法四:(方法三的简化)

# Read from the file file.txt and output the tenth line to stdout.
awk 'NR==10' file.txt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode shell