user797963
user797963

Reputation: 3017

Bash string manipulation, extracting/removing parts

I'm modifying an old bash file and am having some trouble manipulating strings. The problem is that the strings can be anything random to the left of _<date>.<num>. For example, from ThisIsAString-Sub_tag_150827.1, I need to extract _150827.1. In bash, this seems very difficult to do. In any other language, I would split on _, and just grab the last element of the list. How do I do this in bash? I've tried a few different ways (including with awk), but cannot seem to get it right.

Upvotes: 1

Views: 98

Answers (1)

Cyrus
Cyrus

Reputation: 88573

With bash's Parameter Expansion:

a="ThisIsAString-Sub_tag_150827.1"
echo "${a##*_}"

Output:

150827.1

Upvotes: 6

Related Questions