-2

I would like to know how to print specific range of lines using sed or awk and enumerate them like something like this

HORSE
CAT
DOG
LION
MOUSE
MONKEY
SNAKE
PANDA
WOLF
TIGER

so that the output of the line 4-9 looks like this

1. LION
2. MOUSE
3. MONKEY
4. SNAKE
5. PANDA
6. WOLF

Thanks!

Marcus
  • 13
  • 3

2 Answers2

1

Using keywords in the range:

$ awk '/LION/,/WOLF/{print ++c,$0}' file

Output:

1 LION
2 MOUSE
3 MONKEY
4 SNAKE
5 PANDA
6 WOLF

Or using record numbers:

$ awk 'NR==4,NR==9{print ++c,$0}' file
James Brown
  • 36,089
  • 7
  • 43
  • 59
0
cat youdatafile | awk '(NR>=4 && NR<=9) {print($0)}'| awk '{print(NR ". " $0)}'

or shortly

awk 'BEGIN {n=1} (NR>=4 && NR<=9) {print(n ". " $0); n++}' yourdatafile
Slawomir Dziuba
  • 1,265
  • 1
  • 6
  • 13