jere.the.miah
jere.the.miah

Reputation: 3

sed remove last 4 characters

Say I have these 2 lines:

blah:20030717.abc
blah:20040714

Using sed, how can i match on both line start and end only - ^blah.*abc$ - and remove the last 4 characters?

I tried

sed 's/\(^blah.*\)\(.\{4\}$\)/\1/g'

but that removes 4 characters off the other line too.

blah:20030717
blah:2004

and

sed 's/\(^blah.*abc$\)\(.\{4\}$\)/\1/g'

doesn't change anything.

Upvotes: 0

Views: 4635

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336408

Why don't you just do

 sed 's/\.abc$//g' 

since you already know which 4 characters you want to remove?

Or, if you only want to do this in lines that start with blah:,

sed 's/^\(blah.*\)\.abc$/\1/g'

Or, if your question is "How do I remove any final three characters after a dot, if the line starts with blah:":

sed 's/^\(blah.*\)\..\{3\}$/\1/g'

Upvotes: 4

Related Questions