Joni
Joni

Reputation: 375

Replace strings with more than one character

I have a text file like the one shown below:

A B
A D
S F
D TGT
DS K
FDGFDA S
A RE

I want to replace all strings with more than one character (so "TGT", "DS", "FDGFDA" and "RE" above) with a single character (e.g. "X").

Is there a way to do this? It seems like something sed should do easily, but I can't work out the regex string to use to do it!

Upvotes: 2

Views: 79

Answers (3)

Joni
Joni

Reputation: 375

Thanks to both who answered my question: I found a way with another regex:

sed -i 's/[A-Z][A-Z][A-Z]*/X/g'

Upvotes: 1

Lev Levitsky
Lev Levitsky

Reputation: 65791

You can do:

sed 's/^[[:alnum:]]\{2,\}/X/' filename

Upvotes: 2

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

This is the Regex:

\w{2,}

Regular expression visualization

Debuggex Demo

Upvotes: 2

Related Questions