This is a very common task for a sysadmin, but sometimes people forgets how to do this job. Unices provides a large nuber of tools (often distributed under the GNU GPL license) to help sysadmins in their job.


Imagine you have a long text file (i.e. file.txt) and you want to extract 200 lines starting at line 18000.

Let's start:

1 Extracting a range of lines using sed

# sed -n 18000,18199p file.txt


the above command will printout lines in the range [18000, 18200], if you want, you can redirect the output to a new file

# sed -n 18000,18199p file.txt > result.txt



2 Extracting a range of lines using head/tail

head -18199 file.txt | tail -200 > result.txt

 

where

head -N prints the first N lines.

and 

tail -N prints the last N lines.

 


3 Extracting a range of lines using awk

# awk 'NR>=18000 && NR<18200' file.txt > result.txt

 

 

4 Extracting a range of lines using perl

# perl -ne 'print if 18000..18200' file.txt > result.txt



5 Extracting a range of lines using perl

# vi file.txt

and then

:16224,16482w!result.txt

 

Gg1