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==){
print $
}
}
' < file.txt

过程中忘了重定向文件

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

不使用NR

awk '
{
a++;
if(a == ){
b = $;
}
}
END{
if(a >= ){
print b;
}
}
' < file.txt

Tenth Line的更多相关文章

  1. [LeetCode] Tenth Line 第十行

    How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...

  2. [Bash]LeetCode195. 第十行 | Tenth Line

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  3. 195 Tenth Line

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  4. LeetCode OJ:Tenth Line(文件第十行)

    How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...

  5. 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]

    首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...

  6. Leetcode 195 Tenth Line

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  7. [LeetCode] 195. Tenth Line 第十行

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  8. [LeetCode] 195. Tenth Line_Easy tag: Bash

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. HDU-1963

    http://acm.hdu.edu.cn/showproblem.php?pid=1963 完全背包. 题意:给出初始资金,还有年数,然后给出每个物品的购买价格与每年获得的利益,要求在给出的年份后所 ...

  2. Linux学习笔记28——消息队列

    一 关于消息队列 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法,而且,每个数据块都被认为含有一个类型,接收进程可以独立地接受含有不同类型值的数据块.可以通过发送消息来几乎完全避免命名管 ...

  3. NGUI-学习笔记(2)一个项目需求

    using UnityEngine; using System.Collections; public class ins1 : MonoBehaviour { //bool isTarget = f ...

  4. [经典] Best Time to Buy and Sell Stock

    这一系列求最优值的问题变种挺多 1. Say you have an array for which the ith element is the price of a given stock on ...

  5. Kong for Enterprise | Kong - Open-Source API and Microservice Management Layer

    Kong for Enterprise | Kong - Open-Source API and Microservice Management Layer undefined

  6. 【LeetCode】Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. 微软ASP.NET网站部署指南(10):迁移至SQL Server

    1.  综述 第2章的部署SQL Server Compact和第9章的部署数据库更新里解释了为什么终于要升级到完整版SQL Server .本章节将告诉你怎样来做. SQL Server Expre ...

  8. android软键盘的用法总结

    1.软键盘的显示原理 软键盘其实是一个Dialog.InputMethodService为我们的输入法创建了一个Dialog,并且对某些参数进行了设置,使之能够在底部 或者全屏显示.当我们点击输入框时 ...

  9. WCF - 契约

    契约就是双方或多方就某个问题达成的一种的共识  服务提供者通过契约的形式将服务公布出来 服务消费者根据契约来消费 这样通过契约这个中间者就可以规范服务提供的内容 而服务消费者也可以根据契约来使用服务端 ...

  10. jQuery日期联动插件

    此版本为网上的日期联动插件修改版,加入了修改后事件 /* * jQuery Date Selector Plugin * 日期联动选择插件 * * Demo: $("#calendar&qu ...