3

I have a large file, but I need a part of it, the lines between line m (1000) and line n (2000).

How can I get it? (and put it into a new smaller file, or print on screen)

mrdaliri
  • 6,594
  • 21
  • 67
  • 101

4 Answers4

5

Lines between 42 and 96:

sed -n '42,96p'  oldfile > newfile
jim mcnamara
  • 15,023
  • 2
  • 28
  • 45
4

You can do it easily with sed:

sed -n 'm,n p' bigfile > smaller_file

assuming m and n are numbers

Here, you're using:

Sed commands can be given with no addresses, in which case the command will be executed for all input lines; with one address, in which case the command will only be executed for input lines which match that address; or with two addresses, in which case the command will be executed for all input lines which match the inclusive range of lines starting from the first address and continuing to the second address. Three things to note about address ranges: the syntax is addr1,addr2 (i.e., the addresses are separated by a comma); the line which addr1 matched will always be accepted, even if addr2 selects an earlier line; and if addr2 is a regexp, it will not be tested against the line that addr1 matched.

  • The -n option:

    -n, --quiet, --silent
    suppress automatic printing of pattern space

  • The p command:

    Print the current pattern space.

whoan
  • 7,411
  • 4
  • 35
  • 46
2

If file is huge it is better to exit the file browsing as soon as end line is reached. You can use this awk:

awk -v m=10 -v n=15 'NR>n{exit} NR>=m' file > file_chunk
anubhava
  • 664,788
  • 59
  • 469
  • 547
1

use head and tail

head -$start file.txt|tail -$(($end - $start))
Marc B
  • 340,537
  • 37
  • 382
  • 468