BASH : Delete the last X chars from a string

sed delete last x chars

When you scrap data in HTML , it’s common , depending on the quality of you regex to end up with a string with some useless character at the end (Or the Beginning),

If you string looks like that :

My useful data</a> <

You can use the command head to drop the X last character of your string ,
So in my Case , to Keep only the string My useful data and drop the </a> <

I’m going to pipe my string to

This will delete the last 6 chars of my string , giving the data that i need later in my script

but in some implementation of the head command this will not work ,

you can use an alternative with sed

This will delete the last 4 characters of your line ,  or alternatively

 

Leave a Comment

Your email address will not be published. Required fields are marked *