Issue
Input:
i="Item1;Item2;Item3;;;;;;;;"
Desired output:
i="Item1;Item2;Item3"
How do I get rid of the last few semicolons?
I know this one way to do it using 'sed':
sed 's/;$//'
However, it only removes the last semicolon. Running it repeatedly doesn't seem practical.
Solution
You can use
sed 's/;*$//'
The main point here is to add the *
quantifier (that means zero or more) after ;
to make the regex engine match zero or more semi-colons.
Synonymic sed
commands can look like
sed 's/;;*$//' # POSIX BRE "one ; and then zero or more ;s at the end of string"
sed 's/;\+$//' # GNU sed POSIX BRE "one or more semi-colons at the end of string"
sed -E 's/;+$//' # POSIX ERE "one or more semi-colons at the end of string"
Answered By - Wiktor Stribiżew