Tenth Line

来源:互联网 发布:智能电视专用软件 编辑:程序博客网 时间:2024/06/05 16:27

How would you print just the 10th line of a file?

For example, assume that file.txt has the following content:

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

Your script should output the tenth line, which is:

Line 10

Hint:

1. If the file contains less than 10 lines, what should you output?

2. There's at least three different solutions. Try to explore all possibilities.

题目大意:

输出一个文件的第10行。

思考:

1. 如果文件包含内容不足10行,应该输出什么?

2. 有至少3种不同的解决方法,尝试列举所有的可能方案

Bash脚本:

解法一,使用awk

# Read from the file file.txt and output the tenth line to stdout.awk 'NR == 10' file.txt
  • 1
  • 2

解法二,使用sed

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

解法三,组合使用tail与head

# Read from the file file.txt and output the tenth line to stdout.tail -n+10 file.txt | head -n1
0 0
原创粉丝点击