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?
Asked
Active
Viewed 1.6e+01k times
92
-
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 Answers
178

toblerone_country
- 577
- 14
- 26

Greg Hewgill
- 951,095
- 183
- 1,149
- 1,285
-
1Here, 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
-
2Just 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
-
-
3
-
plus 1 for answering the 'with sed' part of the question and then mentioning that `cut` is better – s g Sep 19 '16 at 06:32
16
sed 's/^.\{,5\}//' file.dat

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
-
4BSD 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