bash 使用终端(或外壳)将文件的一部分复制/粘贴到另一个文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9259658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:39:23  来源:igfitidea点击:

Copy/Paste part of a file into another file using Terminal (or Shell)

bashshellunixterminalcopy-paste

提问by WarioBrega

I am trying to copy part of a .txt file from the line number n to the line number n+y (let's say 1000 to 1000000).

我试图将 .txt 文件的一部分从第 n 行复制到第 n+y 行(假设为 1000 到 1000000)。

I tried with operators and sed, and it failed. Here's the command I tried:

我尝试使用运算符和sed,但失败了。这是我试过的命令:

sed -n "1000, 1000000p" path/first/file > path/second/file

回答by FreudianSlip

if you know how many lines are in your source file (wc -l) you can do this .. assume 12000 lines and you want lines 2000 - 7000 in your new file (total of 5000 lines).

如果您知道源文件 (wc -l) 中有多少行,您可以这样做.. 假设 12000 行,并且您希望新文件中有 2000 - 7000 行(总共 5000 行)。

cat myfile | tail -10000 | head -5000 > newfile

猫我的文件| 尾-10000 | 头 -5000 > 新文件

Read the last 10k lines, then read the 1st 5k lines from that.

读取最后 10k 行,然后从中读取第一个 5k 行。

回答by Oscar Per?z

sedcommand should work fine, replace double quotes with single quotes.

sed命令应该可以正常工作,用单引号替换双引号。

sed -n '1000, 1000000p' path/first/file > path/second/file