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. 在centos7 ubuntu15.04 上通过bosh-lite 搭建单机环境cloudfoundry

    Bosh-lite简介       bosh-lite 是一个单机部署cloudfoundry的实验性工具,用于开发人员做poc 验证.Bosh-lite目前支持仅MAC OS X和Linux系统.B ...

  2. laravel调用sql server存储过程并取得ReturnValue

    alter proc [dbo].[aaa](    @AgencyID int,--代理商ID    @AdminID int --结算操作人ID(管理员ID))asbegin    select ...

  3. 根据json对象的值替换json数组里的值

    功能: var fruitArry=[{name:'durian'},{name:'peach'},{name:'banana'},{name:'pitaya'},{name:'apple'},{na ...

  4. 【CF708D】Incorrect Flow 最小费用可行流

    [CF708D]Incorrect Flow 题意:给你一个点数为n,边数为m的流网络,每条边有一个容量c和流量f,这个网络可能是不合法的.你可以花费1的代价使c或f减少或增加1,可以修改无限次.你不 ...

  5. Unity3D笔记 英保通七 物理引擎

    给球体添加刚体RigidBody和球体碰撞器Sphere Collider 效果: OnTriggerEnter() 代码 using UnityEngine; using System.Collec ...

  6. npm使用报错解决办法

    在使用脚手架工具进行项目搭建的时候,很多时候会用到npm ,最近用npm的时候遇到一个错误: 'CALL "I:\Program Files\nodejs\\node.exe" & ...

  7. Shell输入输出重定向

    全部可用的重定向命令列表 命令 说明 command > file 将输出重定向到 file. command < file 将输入重定向到 file. command >> ...

  8. android 常见内存泄漏原因及解决办法

    android常见内存泄漏主要有以下几类: 一.Handler 引起的内存泄漏. 在Android开发中,我们经常会使用Handler来控制主线程UI程序的界面变化,使用非常简单方便,但是稍不注意,很 ...

  9. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  10. MySQL在linux上的二进制安装方法

    建组.建用户: [root@dbking mysql]# groupadd mysql [root@dbking mysql]# useradd -g mysql mysql 解压安装程序: [roo ...