92

I need a one-liner to remove the first five characters on any line of a text file. How can I do that with sed?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
JBeg
  • 1,357
  • 2
  • 10
  • 7
  • similar question http://stackoverflow.com/questions/971879/what-is-a-unix-command-for-deleting-the-first-n-characters-of-a-line – dogbane Sep 25 '10 at 22:00

4 Answers4

178

Use cut:

cut -c6-

This prints each line of the input starting at column 6 (the first column is 1).

toblerone_country
  • 577
  • 14
  • 26
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    Here, cut has a better performance than sed, but I got problems when used on utf-8 encoded characters. – PSchwede Nov 16 '17 at 10:13
  • 2
    Just to elaborate on @PSchwede's comment, GNU `cut` treats all characters as bytes even when you use the `-c` option. GNU `cut` does not support multi-byte characters and probably will not support multi-byte characters for the foreseeable future – Harold Fischer Jan 09 '20 at 02:22
90
sed 's/^.....//'

means

replace ("s", substitute) beginning-of-line then 5 characters (".") with nothing.

There are more compact or flexible ways to write this using sed or cut.

PhilR
  • 5,375
  • 1
  • 21
  • 27
16
sed 's/^.\{,5\}//' file.dat
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 4
    BSD sed does not accept the *at most* bound (`{,5}`) , this is GNU specific. An expression that works on both would be `sed 's/^.\{5\}//' file.dat` – Tristan Feb 26 '15 at 12:15
13
awk '{print substr($0,6)}' file
ghostdog74
  • 327,991
  • 56
  • 259
  • 343