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.
首先要去youtube上面找相关教程看....书到用时方恨少, 哭= =.
然后直到awk(line by line read)的基本用法, 如下:
1) awk '{print}' file.txt    => output all liles line by line
2) awk '/produce/ {print}' file.txt  => output all lines starts with produce
3) awk '/produce/ {print} $2' file.txt  => output all lines starts with produce 的第二个元素/word(空格为界限)
 
然后通过这个post, 我们得到NR, 和FNR的作用.

Awk NR gives you the total number of records being processed or line number. In the following awk NR example, NR variable has line number, in the END section awk NR tells you the total number of records in a file.

$ awk '{print "Processing Record - ",NR;}END {print NR, "Students Records are processed";}' student-marks
Processing Record - 1
Processing Record - 2
Processing Record - 3
Processing Record - 4
Processing Record - 5
5 Students Records are processed

Awk FNR Example: Number of Records relative to the current input file

When awk reads from the multiple input file, awk NR variable will give the total number of records relative to all the input file. Awk FNR will give you number of records for each input file.

$ awk '{print FILENAME, FNR;}' student-marks bookdetails
student-marks 1
student-marks 2
student-marks 3
student-marks 4
student-marks 5
bookdetails 1
bookdetails 2
bookdetails 3
bookdetails 4
bookdetails 5

In the above example, instead of awk FNR, if you use awk NR, for the file bookdetails the you will get from 6 to 10 for each record.

Code
awk 'NR== 10 {print}' file.txt
# or awk 'FNR== 10 {print}' file.txt

[LeetCode] 195. Tenth Line_Easy tag: Bash的更多相关文章

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

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

  2. Leetcode 195 Tenth Line

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

  3. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  4. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  5. [LeetCode] 193. Valid Phone Numbers_Easy tag: Bash

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  6. [LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

    Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...

  7. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  8. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  9. [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

随机推荐

  1. MySQL使用查询结果生成临时表

    MySQL中不支持对同一个表使用其查询结果更新or删除本表内数据(也就是update或delete后的where条件为针对相同表的select),解决方案是创建临时表做过度保存中间数据: 可以直接使用 ...

  2. centos 安装 Vagrant

    使用的软件: 1. CentOS:  CentOS release 6.4 (Final) 2. Vagrant: vagrant_1.2.2_i686.rpm 3. Virtualbox: Virt ...

  3. Linux 如何开启SFTP

    一.SFTP讲解 SFTP 是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法. SFTP 与 FTP有着几乎一样的语法和功能. ...

  4. sencha touch Button Select(点击按钮进行选择)扩展

    此扩展基于官方selectfield控件修改而来,变动并不大,使用方法类似. 代码如下: Ext.define('ux.SelectBtn', { extend: 'Ext.Button', xtyp ...

  5. 部署OpenStack问题汇总(四)--openstack中nova-compute状态status显示为'XXX'的问题

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. 第一次部署openstack的时候就遇见了这个问题,当时的版本是havana, ...

  6. centos6上使用xfs文件系统

    ext4目前也还没有真的支持16TB以上的单分区空间,由于工具的限制,只能创建最大为16T的单分区决定直接用xfs 安装xfs [root@ ~]$ yum install kmod-xfs xfsp ...

  7. 23种设计模式之桥接模式(Bridge)

    桥接模式将抽象部分与它的实现部分分离,使它们都可以独立地变化.它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式.桥接模式类似于多重继承方案,但是 ...

  8. 使用Autolayout xib实现动态高度的TableViewCell

    http://my.oschina.net/u/2360693/blog/481236?p={{totalPage}} 创建Xib文件 首先将Cell做好布局,调整到满意的位置和宽度,然后开始做Aut ...

  9. C语言中的数组的使用——混乱的内存管理

    在C语言中想要创建数组只能自己malloc或者calloc,数组复制则是memcpy. 这样创建出来的数组在调用时是不会检测数组边界的,即你声明了一个长度为5的数组,却可以访问第6个位置……也可以给第 ...

  10. 《C与指针》——高级指针话题

    指针真是让人又爱又恨..... 首先还是先来看一下C语言中的高级指针声明.不要被表面迷惑最重要. /* ** <C和指针>——高级指针话题 */ int i; //定义一个整型变量 int ...