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

推荐订阅源

M
MIT News - Artificial intelligence
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
G
Google Developers Blog
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
月光博客
月光博客
B
Blog
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
博客园_首页
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Jina AI
Jina AI
S
SegmentFault 最新的问题
H
Help Net Security
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News

博客园 - 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"