惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
月光博客
月光博客
人人都是产品经理
人人都是产品经理
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
Vercel News
Vercel News
MyScale Blog
MyScale Blog
爱范儿
爱范儿
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
H
Help Net Security
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
博客园 - 叶小钗
D
Docker

博客园 - GeneJiang

窗口函数的使用 拉链表(Zipper Table)完整总结 —— 数据仓库 SCD Type 2 的经典实现 Kimball 维度建模:数据仓库领域最实用的“用户友好”方法论 DMP 数据接入测试全攻略:从“水龙头”把关到端到端信任 —— 理论解析 + 阿里云 DataWorks、AWS Glue、Azure Data Factory + 自研 EMR(基于 CDH)实战经验 ETL 全链路数据污染与逻辑错误定位实战经验分享 资产损失防控关键点-那些年被薅的“羊毛” 计算广告中常用的计算单位 MVC设计模式(Python) Jupyter NoteBook 的快捷键使用指南 Hive常用函数 Hive Tutorial(一) - Hive的数据类型 基于TestNG和Maven的接口测试之(一)- 基础配置 Python常用库之Requests自我总结 Linux基本命令(一) Presto Step by Step之一Presto简介 python 判断字符串是否包含子字符串 Python常用的排序 CentOS7安装(三)- 配置阿里云yum源 OSQA的配置 MySQL学习 (三) Limit-Distinct-Union
195 Tenth line-取第十行
GeneJiang · 2019-06-15 · via 博客园 - GeneJiang

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

解决方案

  1. 使用head 和 tail
tail -n+10 file.txt | head -1
  1. 使用sed
sed '10p' file.txt
  1. 使用awk
awk 'NR==10' file.txt
  1. 使用脚本
no=0                                                              
while IFS= read -r line                                          
do  
    ((no=$no+1))
    if [[ $no -eq 10 ]]                                             
    then                                                        
        echo $line
	break
    fi                                                      
done < "file.txt"