Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move top 1000 lines from text file to a new file using Unix shell commands

Tags:

shell

unix

copy

I wish to copy the top 1000 lines in a text file containing more than 50 million entries, to another new file, and also delete these lines from the original file.

Is there some way to do the same with a single shell command in Unix?

like image 380
gagneet Avatar asked Apr 29 '09 05:04

gagneet


People also ask

How do I find the first 100 lines of a file in Linux?

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>. By default, head shows you the first 10 lines of a file. You can change this by typing head -number filename, where number is the number of lines you want to see.

Which command is used to display top 10 lines in the specified file?

The head command, as the name implies, print the top N number of data of the given input. By default, it prints the first 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.


2 Answers

head -1000 input > output && sed -i '1,+999d' input

For example:

$ cat input 
1
2
3
4
5
6
$ head -3 input > output && sed -i '1,+2d' input
$ cat input 
4
5
6
$ cat output 
1
2
3
like image 163
moinudin Avatar answered Oct 19 '22 20:10

moinudin


head -1000 file.txt > first100lines.txt
tail --lines=+1001 file.txt > restoffile.txt
like image 26
cletus Avatar answered Oct 19 '22 18:10

cletus