I would like to remove any ABC at the end of the string.
The best I have came up with is
echo ${String}| sed -e 's/["ABC"]*$//g'
However, it will remove all the A, or B or C at the end of the string.
If String is DAAAAABCBBBCCABCABC, if I use the above expression, it will return "D", instead of "DAAAAABCBBBCC"
Is there any better way of doing this? Thanks.
To remove the last n characters of a string, we can use the parameter expansion syntax ${str::-n} in the Bash shell. -n is the number of characters we need to remove from the end of a string.
Remove Character from String Using trThe tr command (short for translate) is used to translate, squeeze, and delete characters from a string. You can also use tr to remove characters from a string. For demonstration purposes, we will use a sample string and then pipe it to the tr command.
To remove the first and last character of a string, we can use the parameter expansion syntax ${str:1:-1} in the bash shell. 1 represents the second character index (included). -1 represents the last character index (excluded). It means slicing starts from index 1 and ends before index -1 .
bash can do this internally. The following removes any "ABC" string at the end, and its result can used in variable assignment, a command or whatever:
${String%ABC}
You can also use a regex, not just a simple string match. See http://tldp.org/LDP/abs/html/string-manipulation.html
This should work:
echo "DAAAAABCBBBCCABCABC" | sed -e 's/\(ABC\)*$//g'
Result:
DAAAAABCBBBCC
Surround string between parentheses and *
applies to all letters inside them in that exact order.
You should use:
sed -E 's/(ABC)+$//'
OR:
sed -r 's/(ABC)+$//'
Both will give output:
DAAAAABCBBBCC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With