Question

If the character is met more than once I must replace it with a single one. For example i have this in a file: ????aca, I must replace it with ?aca. I've tried it with tr, but didn't make it to work proper.

Was it helpful?

Solution

It seems easy with using backreferences:

sed 's/\(.\)\1\+/\1/g' infile

OTHER TIPS

If you want to truncate consecutive runs of one particular character, tr -s does that.

tr -s '?' <file >file.new

Based on your limitation to ?

sed 's/[?]\{2,\}/?/g' YourFile

also POSIX compliant

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top