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. 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.

解法:

awk ‘
{
    if(NR==10){
        print $0
    }
}
‘ < file.txt

过程中忘了重定向文件

awk ‘
{
   a++;
   if(a == 10){
       print $0;
   }
}
‘ < file.txt

不使用NR

awk ‘
{
   a++;
   if(a == 10){
       b = $0;
   }
}
END{
    if(a >= 10){
        print b;
    }
}
‘ < file.txt
时间: 2024-10-21 05:53:09

Tenth Line的相关文章

[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第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: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

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

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】关于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

leetcode数据库sql之Department Top Three Salaries

leetcode原文引用: 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: Li