Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete the last 2 characters from a text file: shell script

I need to delete the last 2 characters from a text file in shell script. Any idea about how I can do it?

like image 899
user1835630 Avatar asked Dec 20 '22 08:12

user1835630


1 Answers

Delete the last two characters on the last line only with sed:

$ sed '$s/..$//' file

If you are happy with changes then use -i to store them back to the file:

$ sed -i '$s/..$//' file

If you wanted to delete the last two characters on every line it would be:

$ sed 's/..$//' file

Again use -i to store the changes back to the file:

$ sed -i 's/..$//' file
like image 85
Chris Seymour Avatar answered Dec 28 '22 06:12

Chris Seymour