Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete the first five characters on any line of a text file in Linux with sed

Tags:

linux

unix

sed

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?

like image 636
JBeg Avatar asked Sep 25 '10 20:09

JBeg


People also ask

How do you delete a line in a file using sed?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

How do I remove the first 10 characters from a string in Unix?

Removing the first n characters To remove the first n characters of a string, we can use the parameter expansion syntax ${str: position} in the Bash shell.

Can we delete content in a file by using sed command?

There is no available to delete all contents of the file. How to delete all contents of the file using sed command.


2 Answers

Use cut:

cut -c6- 

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

like image 54
Greg Hewgill Avatar answered Sep 28 '22 09:09

Greg Hewgill


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.

like image 20
PhilR Avatar answered Sep 28 '22 10:09

PhilR