WarioBrega
WarioBrega

Reputation: 195

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

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).

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

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

Upvotes: 13

Views: 29670

Answers (2)

Oscar Perëz
Oscar Perëz

Reputation: 31

sed command should work fine, replace double quotes with single quotes.

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

Upvotes: 3

FreudianSlip
FreudianSlip

Reputation: 2920

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).

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

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

Upvotes: 24

Related Questions