Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove last 14 digits from string and a underscore, if there are 14 digits

Tags:

regex

bash

I have a string like this:

data-c(huk24-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(huk24)_20130620200658

where the timestamp with 14 digits and the underscore should be removed. So it should look like this:

data-c(huk24-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(huk24)

How can I achieve this in a bash script? Note that removing should only happen, when there really is an underscore and 14 digits.

like image 357
mles Avatar asked Jun 20 '13 18:06

mles


1 Answers

Use parameter expansion:

string=${string%_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]}
like image 114
choroba Avatar answered Nov 28 '22 12:11

choroba