Rob Adams
Rob Adams

Reputation: 33

sed delete trailing pattern of digits

I have a .txt file where the last column includes a number pattern after the text like 'Baker 2-13' or 'Charlie 03-144.' I would like to remove all the digits at the end of the line, and just be left with Baker and Charlie. I have tried piping the sed command at the end of my awk statement, with no success.

sed -E 's/[0-9]{1,2}"-"[0-9]{1,3}$//'

I've tried adding the space and carriage returns to my sed command, but still no luck.

sed -E 's/[0-9]{1,2}"-"[0-9]{1,3}\s\r$//'

I've also tried this, but it only works when I echo a text sample, it doesn't work on each line of my .txt file

echo "CHARLIE 02-157" | sed -E 's/[0-9]*([0-9])+\-[0-9]*([0-9])+$//'

Any ideas?

Upvotes: 1

Views: 657

Answers (2)

josifoski
josifoski

Reputation: 1726

Simple sed solution

sed 's/[- 0-9]*$//'  

This will delete trailing dashes, blanks and numbers!

Upvotes: 0

anubhava
anubhava

Reputation: 784898

This should work:

sed -i.bak -E 's/[0-9]{1,2}-[0-9]{1,3}$//' file

cat file
Baker
Charlie

You don't need to quote hyphen in the pattern.

Upvotes: 2

Related Questions