86

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.
  • 38,596
  • 16
  • 96
  • 104
JBeg
  • 1,277
  • 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

157

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
  • 890,778
  • 177
  • 1,125
  • 1,260
  • 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
  • 1
    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
86
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,145
  • 1
  • 18
  • 25
16
sed 's/^.\{,5\}//' file.dat
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
  • 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
12
awk '{print substr($0,6)}' file
ghostdog74
  • 307,646
  • 55
  • 250
  • 337