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.4e+01k times
86
-
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
157
toblerone_country
- 577
- 14
- 26
Greg Hewgill
- 890,778
- 177
- 1,125
- 1,260
-
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
-
1Just 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
-
-
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
- 740,318
- 145
- 1,296
- 1,325
-
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