Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to use sed in the terminal (no other option, I'm afraid) and I need to find and replace all sequences of four or more digits (no white spaces) with a specific word "brad". I have tried and tried to understand the manual but it is just so bad.

How do I do this? And while I am looking for the exact command, I would appreciate an explanation of why it works the way it does (the exact command used here that is).

share|improve this question
already answered, but if you want to learn about might find this more informative then the man pages: – RobotHumans Mar 16 '12 at 10:56
up vote 2 down vote accepted

One way:

sed 's/[0-9]\{4,\}/brad/g' infile


[0-9]              # Any digit.
\{4,\}             # Matched more or equal to 4 times.
brad               # Substitute matched string with literal 'brad'
/g                 # Apply it globally: Many times for each line.
share|improve this answer
Thank you. I managed to get the s/[0-9]/brad/g before posting, but \{4,\} ... is that generally true in shell commands ? or is it "just here" ? – Kalec Mar 16 '12 at 10:40
@Kalec: It depends. There are several regex flavours but syntax should be similar. – Birei Mar 16 '12 at 10:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.