我有一个~23000行的SQL转储,其中包含几个数据库的数据价值。我需要提取这个文件的某个部分(即单个数据库的数据),并将其放在一个新文件中。我知道我想要的数据的开始行号和结束行号。
谁知道一个Unix命令(或一系列命令)可以从文件中提取16224到16482行之间的所有行,然后将它们重定向到一个新文件中?
我有一个~23000行的SQL转储,其中包含几个数据库的数据价值。我需要提取这个文件的某个部分(即单个数据库的数据),并将其放在一个新文件中。我知道我想要的数据的开始行号和结束行号。
谁知道一个Unix命令(或一系列命令)可以从文件中提取16224到16482行之间的所有行,然后将它们重定向到一个新文件中?
当前回答
您可以使用'vi',然后使用以下命令:
:16224,16482w!/tmp/some-file
另外:
cat file | head -n 16482 | tail -n 258
编辑:-只是补充解释,您使用head -n 16482显示前16482行,然后使用tail -n 258显示第一次输出的最后258行。
其他回答
我正准备发布头部/尾部技巧,但实际上我可能只是启动emacs。: -)
Esc-x goto-line ret 16224 马克(ctrl-space) Esc-x goto-line ret 16482 esc-w
打开新的输出文件ctl-y 保存
让我看看发生了什么。
Sed -n '16224,16482p' < dump.sql
我会用:
awk 'FNR >= 16224 && FNR <= 16482' my_file > extracted.txt
FNR包含从文件中读取的行的记录(行)号。
那些想要计算头部、|、尾部组合的间隔的人是想多了。
下面是如何在不计算任何东西的情况下得到“16224 - 16482”范围:
cat file | head -n +16482 | tail -n +16224
解释:
The + instructs the head/tail command to "go up to / start from" (respectively) the specified line number as counted from the beginning of the file. Similarly, a - instructs them to "go up to / start from" (respectively) the specified line number as counted from the end of the file The solution shown above simply uses head first, to 'keep everything up to the top number', and then tail second, to 'keep everything from the bottom number upwards', thus defining our range of interest (with no need to compute an interval).
艾德:使用
ed -s infile <<<'16224,16482p'
-s抑制诊断输出;实际的命令在一个here-string中。具体来说,16224,16482p在所需的行地址范围上运行p (print)命令。