Remove the last 2 characters from the text file: shell script

I need to remove the last 2 characters from a text file in a shell script. Any idea on how I can do this?

+4
source share
1 answer

Delete the last two characters in the last line with sed :

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

If you are happy with the changes, use -i to save them back to the file:

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

If you want to delete the last two characters in a line each , this will be:

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

Use -i again to save the changes back to the file:

 $ sed -i 's/..$//' file 
+10
source

Source: https://habr.com/ru/post/1469267/


All Articles