-4

Is it possible to retrieve data from lines number 1, 11, 21 ,31 from a text file using Linux commands?

I need to do the same for 2, 12, 22, 32 and so on.

Mat
  • 188,820
  • 38
  • 367
  • 383
vishnu
  • 451
  • 4
  • 18
  • 2
    A little search would have given you a solution : http://stackoverflow.com/questions/83329/how-can-i-extract-a-range-of-lines-from-a-text-file-on-unix – Denys Séguret Apr 29 '12 at 07:50
  • The 'a little search' solution is for discreet ranges of lines, which is a different problem from the one posed here. None of the solutions in [SO 83329](http://stackoverflow.com/questions/83329/how-can-i-extract-a-range-of-lines-from-a-text-file-on-unix) comes close to tackling this problem. – Jonathan Leffler Apr 29 '12 at 07:57
  • @Leffler Are you joking ? Should the answer also be rewriten for lines 1, 11, 21 and 31 because thinking 4 seconds or typing man is painful ? – Denys Séguret Apr 29 '12 at 08:04
  • 1
    @dystroy: I agree with Jonathan. What in the answers to that question shows how to select every nth line in a file? (I'm _not_ saying this hasn't been asked before - probably has, but the question you link to doesn't answer this question.) – Mat Apr 29 '12 at 08:25

1 Answers1

5

You can use awk for this:

awk '(NR % 10 == 1){ print }' your_input_file

For example:

 $ seq 1 100|awk '(NR%10 == 2){print}'
2
12
22
32
42
52
62
72
82
92

As glenn jackman points out, you can parametrize the awk script to make it more easy to use. And given that print is the default action, you can simply write:

 $ seq 1 20|awk -v step=10 -v idx=3 'NR%step==idx'
3
13
Community
  • 1
  • 1
Mat
  • 188,820
  • 38
  • 367
  • 383