User1291
User1291

Reputation: 8209

bash - split but only use certain numbers

let's say I want to split a large file into files that have - for example - 50 lines in them

split <file> -d -l 50 prefix

How do I make this ignore the first n and the last m lines in the <file>, though?

Upvotes: 1

Views: 53

Answers (3)

oliv
oliv

Reputation: 13259

If n and m have the start and the end line number to print, you can do this

with sed

sed -n $n,${m}p file

-n avoid printing by default all lines. p is printing only the line that matches the range indicated by $n,${m}


With awk

awk "NR>$n && NR<$m" file

where NR represent the number of line

Upvotes: 1

Inian
Inian

Reputation: 85800

You can use awk over the file that is splitted, by provide a range of lines you need.

awk -v lineStart=2 -v lineEnd=8 'NR>=lineStart && NR<=lineEnd' splitted-file

E.g.

$ cat line
1
2
3
4
5
6
7
8
9
10

The awk with range from 3-8 by providing

$ awk -v lineStart=3 -v lineEnd=8 'NR>=lineStart && NR<=lineEnd' file
3
4
5
6
7
8

Upvotes: 1

Bart Friederichs
Bart Friederichs

Reputation: 33533

Use head and tail:

tail -n +N [file] | head -n -M | split -d -l 50

Ex (lines is a textfile with 10 lines, each with a consecutive number):

[bart@localhost playground]$ tail -n +3 lines | head -n -2
3
4
5
6
7
8

Upvotes: 1

Related Questions