195 Tenth Line

Given a text file file.txt, print just the 10th line of the file.

Example:

Assume that file.txt has the following content:

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

Your script should output the tenth line, which is:

Line 10

Note:
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行

#从第10行开始显示,然后取第一行
tail -n +10 file.txt | head -n 1
#打印第10行
sed -n 10p file.txt
awk ‘NR==10 {print $0}‘ file.txt
awk ‘NR==10 ‘ file.txt
cnt=0
while read line
do
    let cnt++
    if [ $cnt -eq 10 ];then
      echo $line
      exit 0
    fi
done < file.txt

原文地址:https://www.cnblogs.com/mengchunchen/p/9301063.html

时间: 2024-10-12 16:09:14

195 Tenth Line的相关文章

Leetcode 195 Tenth Line

Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Your script should output the tenth line, which is: Line

[ LeetCode试题 ] 195 Tenth Line

Solution1: cat file.txt | awk '{if(NR==10)print $0}' Solution2: cat file.txt | sed -n '10p'

LeetCode第195题---Tenth Line

How would you print just the 10th line of a file? For example, assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Your script should output the tenth line, which is: Line 10 [show hi

[LeetCode][Shell]Tenth Line

Tenth Line How would you print just the 10th line of a file? For example, assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Your script should output the tenth line, which is: Line

[LeetCode] Tenth Line 第十行

How would you print just the 10th line of a file? For example, assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Your script should output the tenth line, which is: Line 10 [show hi

【leetcode~Shell】:Tenth Line

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 r

Tenth Line

How would you print just the 10th line of a file? For example, assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Your script should output the tenth line, which is: Line 10 Hint: 1.

【LeetCode】关于shell的几个问题

195. Tenth Line 输出file.txt中的第十行 答案: # Read from the file file.txt and output the tenth line to stdout. #!/bin/bash sed -n '10p' file.txt 193. Valid Phone Numbers 找出file.txt中符合(xxx) xxx-xxxx or xxx-xxx-xxxx格式的电话号码 答案: 1 cat file.txt | grep -Eo '^(\([0

[Linux] 输出文件的指定行

1.获取第k行(以k=10为例) 要注意的是,如果文件包含内容不足10行,应该不输出. # Read from the file file.txt and output the tenth line to stdout. # 解法一,使用awk awk 'NR == 10' file.txt # 解法二,使用sed(个人测试结果:sed方法比awk快) sed -n '10p' file.txt # 解法三,组合使用tail与head tail -n+10 file.txt | head -n1