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. 最小生成树(prime算法 & kruskal算法)和 最短路径算法(floyd算法 & dijkstra算法)

    一.主要内容: 介绍图论中两大经典问题:最小生成树问题以及最短路径问题,以及给出解决每个问题的两种不同算法. 其中最小生成树问题可参考以下题目: 题目1012:畅通工程 http://ac.jobdu ...

  2. Node.j中path模块对路径的操作

    一.path模块 https://nodejs.org/docs/latest/api/path.html#path_path_join_paths 1.join方法 ==> 该方法将多个参数值 ...

  3. 2-3 vue配置介绍

    一.通过vue-cli构建的项目的文件介绍 1.bulid文件夹 ==> 项目打包的配置文件夹 2.config文件夹 ==> 打包的配置 3.src文件夹 ==> 项目开发的源码 ...

  4. 如何在Computer下添加System Folder

    1.创建一个GUID标识该系统目录,比如:{7854FF7A-470F-4D04-9FC5-4CFC7B2A0E89}.下面的操作步骤将全部使用这个示例GUID. 2.打开注册表编辑器(Registr ...

  5. 源码包安装Python3.6

    1,安装Python3.6的依赖包 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel r ...

  6. CodeFrist、ModelFirst、DatabaseFirst

    一.CodeFirst 使用System.Data.Entity.DbContext与System.Data.Entity.DbSet构建的数据模型,没有可视化文件但只有实体类的称为CodeFirst ...

  7. 7.18python进程池

    主要方法  常用的就是   from multiprocessing import Pool map() 同步 apply() 同步 apply_async()  异步  手动 close()  jo ...

  8. css 多行文字,超出部分隐藏,...代替

    css虽然简单,但其实也是记得常用的那些,不常用的还是要搜一搜再写

  9. windows下gcc的安装

    首先打开 www.mingw.org .   www.mingw.org 直接点击右上方的 Download Installer 即可下载.   点击 Download Installer 进入下载页 ...

  10. 更改hosts操作

    更改hosts操作: 1.打开文件夹目录:/etc/hosts 2.复制hosts文件 3.修改复制的文件 4.移动并替换到原来的hosts文件 5.鉴定输入密码操作成功 具体修改方案使用: 0.0. ...