庆典工具从文件中获得第n行 [英] bash tool to get nth line from a file

查看:212
本文介绍了庆典工具从文件中获得第n行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有这样做的一个规范的方式?我一直在使用头-n |尾-1 这是卓有成效的,但如果有从一个文件一个bash工具,专门提取线(或行范围)我一直在想。

Is there a "canonical" way of doing that? I've been using head -n | tail -1 which does the trick, but I've been wondering if there's a bash tool that specifically extracts a line (or a range of lines) from a file.

由规范我的意思,其主要功能是这样做的。一个节目

By "canonical" I mean a program whose main function is doing that.

推荐答案

和管道与将慢一个巨大的文件。我建议 SED 是这样的:

head and pipe with tail will be slow for a huge file. I would suggest sed like this:

sed 'NUMq;d' file

其中, NUM 是要打印行数;因此,例如, sed的'10Q; D'文件打印 10号线文件

Where NUM is the number of the line you want to print; so, for example, sed '10q;d' file to print the 10th line of file.

说明:

NUMq 当行号将立即退出 NUM

D 将删除行而不是打印出来;这是最后一行抑制,因为导致脚本的其余部分退出的时候被跳过。

d will delete the line instead of printing it; this is inhibited on the last line because the q causes the rest of the script to be skipped when quitting.

如果您有 NUM 在一个变量,你将要使用双引号代替单:

If you have NUM in a variable, you will want to use double quotes instead of single:

sed "${NUM}q;d" file

这篇关于庆典工具从文件中获得第n行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆