列表

详情


195. 第十行

给定一个文本文件 file.txt,请只打印这个文件中的第十行。

示例:

假设 file.txt 有如下内容:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

你的脚本应当显示第十行:

Line 10

说明:
1. 如果文件少于十行,你应当输出什么?
2. 至少有三种不同的解法,请尝试尽可能多的方法来解题。

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
# Read from the file file.txt and output the tenth line to stdout.

bash 解法, 执行用时: 8 ms, 内存消耗: N/A, 提交时间: 2018-08-21 18:36:05

# Read from the file file.txt and output the tenth line to stdout.
aa=`wc -l file.txt | awk '{print $1}'`
if [ $aa -lt 10 ]
then
    echo ''
else
    head -10 file.txt | tail -1
fi

bash 解法, 执行用时: 8 ms, 内存消耗: N/A, 提交时间: 2018-08-21 18:28:08

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

bash 解法, 执行用时: 8 ms, 内存消耗: N/A, 提交时间: 2018-08-20 18:08:14

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

上一题