Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get n lines from file which are equal spaced

Tags:

bash

sed

I have a big file with 1000 lines.I wanted to get 110 lines from it. Lines should be evenly spread in Input file.

For example,I have read 4 lines from file with 10 lines

Input File

1
2
3
4
5
6
7
8
9
10

outFile:

1
4
7
10
like image 235
abilng Avatar asked Dec 19 '22 05:12

abilng


1 Answers

Use:

sed -n '1~9p' < file

The -n option will stop sed from outputting anything. '1~9p' tells sed to print from line 1 every 9 lines (the p at the end orders sed to print).

To get closer to 110 lines you have to print every 9th line (1000/110 ~ 9).


Update: This answer will print 112 lines, if you need exactly 110 lines, you can limit the output just using head like this:

sed -n '1~9p' < file | head -n 110
like image 115
higuaro Avatar answered Dec 21 '22 22:12

higuaro