Joseph Rocha
Joseph Rocha

Reputation: 13

Delete Repeated Characters without back-referencing with SED

Let's say we have a file that contains

HHEELLOO
HHYYPPOOTTHHEESSIISS

and we want to delete repeated characters. To my knowledge we can do this with

s/\([A-Z]\)\1/\1/g

This is a homework problem and the professor said he wants us to try the exercises without back-referencing or extended regular expressions. Is that possible on this one? I would appreciate it if anyone could point me in the right direction, thanks!

Upvotes: 1

Views: 77

Answers (2)

Ed Morton
Ed Morton

Reputation: 203674

The only reasonable way to do this is to use the right tool for the job, in this case tr:

$ tr -s 'A-Z' < file
HELO
HYPOTHESIS

If you were going to use sed for that specific problem though then it'd just be:

$ sed 's/\(.\)./\1/g' file
HELO
HYPOTHESIS

If that's not what you're looking for then edit your question to show more truly representative sample input and expected output.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249253

Here's one way:

s/AA/A/g
s/BB/B/g
...
s/ZZ/Z/g

As a one-liner:

sed 's/AA/A/g; s/BB/B/g; ...'

Upvotes: 1

Related Questions