print or remove lines of a file

Show one line by its number, a range, skip the first ones, or take off the last.

  • text

Steps

  1. sed -n '5p' file.txt

    line 5

  2. sed -n '5,10p' file.txt

    lines 5 to 10

  3. tail -n +2 file.txt

    everything from line 2: skip a header

  4. head -n 20 file.txt

    the first 20 lines

  5. tail -n 20 file.txt

    the last 20 lines

  6. sed -i '$d' file.txt

    remove the last line

  7. head -n -5 file.txt

    everything but the last 5 lines (GNU)

Commands used

  • sed
  • head
  • tail
  • awk

Each has a page in lore: lore sed opens it.