194

If I use cat -n text.txt to automatically number the lines, how do I then use the command to show only certain numbered lines.

3
  • 2
    What do you mean with "show only certain numbered lines", can you put an expected output?
    – tachomi
    Commented Jun 8, 2016 at 19:27
  • 1
    There's probably going to be a million ways to do this. tail + head can also do this, as can awk
    – Bratchley
    Commented Jun 8, 2016 at 19:36
  • Note to self: create a python script for this and release it to the public :) Commented Feb 10, 2022 at 20:21

6 Answers 6

318

Use sed

Usage

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

To print one line (5)

$ sed -n 5p file
Line 5

To print multiple lines (5 & 8)

$ sed -n -e 5p -e 8p file
Line 5
Line 8

To print specific range (5 - 8)

$ sed -n 5,8p file
Line 5
Line 6
Line 7
Line 8

To print range with other specific line (5 - 8 & 10)

$ sed -n -e 5,8p -e 10p file
Line 5
Line 6
Line 7
Line 8
Line 10
1
  • 1
    Note that for a range including the first line (for example first 5 lines), you'll use sed -n 1,5p ... not sed -n 0,5p .... At least for the version of sed installed by default on Mac OS 10.15.
    – Jason V.
    Commented Aug 3, 2022 at 17:53
56

One way of doing it is by using sed:

cat -n text.txt | sed '11d'

where 11 is the number of the line you want removed.

Or to remove all but 11:

cat -n text.txt | sed '11!d'

Ranges are also possible:

cat -n text.txt | sed '9,12!d'

And cat -n isn't even needed:

sed '9,12!d' text.txt
1
  • Yes, cat -n isn't needed, but you are gonna have it without line numbers.
    – czende
    Commented Oct 31, 2022 at 13:37
49

You can use awk straight up.

awk 'NR==1' file.txt

replacing '1' with the desired line number.

3
  • 1
    This is by a million miles the best answer here and deserves a lot more love than it has.
    – Wren
    Commented Nov 4, 2018 at 15:56
  • Simple solution for 50ish years. Good post.
    – Cymatical
    Commented Jul 15, 2021 at 15:05
  • I concur that this is the very best answer. To add on top you can use equivalence operators to filter data out ie. awk 'NR>=20' file.txt
    – Eric
    Commented Apr 19, 2023 at 4:27
22

Depending on goals I like head or grep

cat /var/log/syslog -n | head -n 50 | tail -n 10

will return lines 41 thru 50.

or

cat /var/log/syslog -n | grep " 50" -b10 -a10

will show lines 40 thru 60. The problem with the grep method is that you have to use account for padding of the line numbers (notice the space)

Both are quite handy for parsing log files.

5
  • Neither example needs cat though Commented Jun 9, 2016 at 9:12
  • 2
    well yeah... but.... but... ..... There are better ways. The question asked about using cat though, so I used it.
    – coteyr
    Commented Jun 9, 2016 at 9:22
  • cat can't do what the OP wants Commented Jun 9, 2016 at 9:33
  • 1
    Nice tricks. Tx.
    – Cymatical
    Commented Jul 15, 2021 at 15:06
  • loved the grep "foo" -b10 -a10 ! Very useful
    – Kojo
    Commented Feb 9, 2022 at 17:45
5

As others have shown you, there is no need to use cat -n. Other programs will do it for you. If, however, you really need to parse the output of cat -n and show only specific lines (for example, 4-8, 12 and 42), you could do:

$ cat -n file | awk '$1>=4 && $1<=8 || $1==12 || $1==42'
 4  Line 4
 5  Line 5
 6  Line 6
 7  Line 7
 8  Line 8
12  Line 12
42  Line 42

In awk, $1 is the first field, so this command prints all lines whose first fields are i) between 4 and 8 (inclusive) or ii) 12 or iii) 42.

If you also want to remove the field added by cat -n to get the original lines from the file, you can do:

$ cat -n file | awk '$1>=4 && $1<=8 || $1==12 || $1==42{sub(/^\s*[0-9]+\s*/,""); print}'
Line 4
Line 5
Line 6
Line 7
Line 8
Line 12
Line 42
2
  • Couldn't you use NR instead of $1? Commented May 27, 2020 at 18:57
  • @SolomonUcko sure, but since I was demonstrating how to do this using cat -n specifically, I thought I would use the output that cat -n provides.
    – terdon
    Commented May 27, 2020 at 18:59
3

You can use sed to show only one number and loop this with a for loop:

for line in 1 3 7 11; do sed -n ${line}p text.txt; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .