Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing pattern at the end of a string using sed or other bash tools

Tags:

regex

bash

sed

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.

like image 640
Kay Chan Avatar asked Feb 08 '12 09:02

Kay Chan


People also ask

How do I remove the last character of a string in Bash?

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.

How do I remove something from a string in Bash?

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.

How do you remove the first and last character of a string in Bash?

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 .


3 Answers

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

like image 56
mabraham Avatar answered Oct 19 '22 10:10

mabraham


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.

like image 23
Birei Avatar answered Oct 19 '22 10:10

Birei


You should use:

sed -E 's/(ABC)+$//'

OR:

sed -r 's/(ABC)+$//'

Both will give output:

DAAAAABCBBBCC
like image 5
anubhava Avatar answered Oct 19 '22 09:10

anubhava