1

I have this so far:

sed -n '0,10p' yourfile > newfile

But it is not working, just outputs a blank file :(

John
  • 5,139
  • 19
  • 57
  • 62
  • Please provide a sample input and expected output. "Remove every 10 lines" is rather ambiguous... – thkala Apr 04 '11 at 23:03
  • 1
    do you want every ***10th*** line removed, or you just want the first 10 lines of a file or you want to see only the 10th line of each file? – SiegeX Apr 04 '11 at 23:04
  • 1
    out of curiosity, which of the 1-liners in my answer were you looking for? – SiegeX Apr 05 '11 at 00:05
  • Possibly related: https://unix.stackexchange.com/questions/369181/printing-every-nth-line-out-of-a-large-file-into-a-new-file – Gabriel Devillers Nov 19 '18 at 15:53

4 Answers4

4

Your question is ambiguous, so here is every permutation I can think of:

Print only the first 10 lines

head -n10 yourfile > newfile

Skip the first 10 lines

tail -n+10 yourfile > newfile

Print every 10th line

awk '!(NR%10)' yourfile > newfile

Delete every 10th line

awk 'NR%10' yourfile > newfile
SiegeX
  • 135,741
  • 24
  • 144
  • 154
3

(Since an ambiguous questions can only have an ambiguous answer...)

To print every tenth line (GNU sed):

$ seq 1 100 | sed -n '0~10p'
10
20
30
40
...
100

Alternatively (GNU sed):

$ seq 1 100 | sed '0~10!d'
10
20
30
40
...
100

To delete every tenth line (GNU sed):

$ seq 1 100 | sed '0~10d'
1
...
9
11
...
19
21
...
29
31
...
39
41
...

To print the first ten lines (POSIX):

$ seq 1 100 | sed '11,$d'
1
2
3
4
5
6
7
8
9
10

To delete the first ten lines (POSIX):

$ seq 1 100 | sed '1,10d'
11
12
13
14
...
100
thkala
  • 84,049
  • 23
  • 157
  • 201
0

With non-GNU sed, to print every 10th line use

sed '10,${p;n;n;n;n;n;n;n;n;n;}'

(GNU : sed -n '0~10p')

and to delete every 10th line use

sed 'n;n;n;n;n;n;n;n;n;d;'

(GNU : sed -n '0~10d')

tricasse
  • 1,299
  • 13
  • 18
0
python -c "import sys;sys.stdout.write(''.join(line for i, line in enumerate(open('yourfile')) if i%10 == 0 ))" >newfile

It is longer, but it is a single language - not different syntax and aprameters for each thing one tries to do.

jsbueno
  • 99,910
  • 10
  • 151
  • 209