如何从 Unix 上的文本文件中提取预定范围的行? [英] How can I extract a predetermined range of lines from a text file on Unix?

查看:29
本文介绍了如何从 Unix 上的文本文件中提取预定范围的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ~23000 行的 SQL 转储,其中包含多个数据库的数据.我需要提取此文件的某个部分(即单个数据库的数据)并将其放入一个新文件中.我知道我想要的数据的开始和结束行号.

I have a ~23000 line SQL dump containing several databases worth of data. I need to extract a certain section of this file (i.e. the data for a single database) and place it in a new file. I know both the start and end line numbers of the data that I want.

有谁知道一个 Unix 命令(或一系列命令)可以从文件中提取 16224 和 16482 行之间的所有行,然后将它们重定向到一个新文件中?

Does anyone know a Unix command (or series of commands) to extract all lines from a file between say line 16224 and 16482 and then redirect them into a new file?

推荐答案

sed -n '16224,16482p;16483q' filename > newfile

来自 sed 手册:

p -打印出模式空间(到标准输出).此命令通常仅与 -n 命令行选项结合使用.

p - Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.

n -如果未禁用自动打印,则打印模式空间,然后无论如何用下一行输入替换模式空间.如果没有更多的输入然后 sed 退出而不进行任何处理命令.

n - If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands.

q -退出 sed 而不处理任何命令或输入.请注意,如果未使用 -n 选项禁用自动打印,则会打印当前模式空间.

q - Exit sed without processing any more commands or input. Note that the current pattern space is printed if auto-print is not disabled with the -n option.

sed 脚本中的地址可以是以下任何一种形式:

Addresses in a sed script can be in any of the following forms:

数量指定行号将仅匹配输入中的该行.

number Specifying a line number will match only that line in the input.

一个地址范围可以通过指定两个地址来指定用逗号 (,) 分隔.地址范围匹配从以下位置开始的行其中第一个地址匹配,并一直持续到第二个地址匹配(包括).

An address range can be specified by specifying two addresses separated by a comma (,). An address range matches lines starting from where the first address matches, and continues until the second address matches (inclusively).

这篇关于如何从 Unix 上的文本文件中提取预定范围的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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