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

推荐订阅源

A
About on SuperTechFans
量子位
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
V
Visual Studio Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
V
V2EX
The GitHub Blog
The GitHub Blog
博客园_首页
月光博客
月光博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
博客园 - 叶小钗
F
Fortinet All Blogs
T
Tailwind CSS Blog
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
WordPress大学
WordPress大学
B
Blog
H
Help Net Security

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