Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 sed...you might find this more informative then the man pages: ibm.com/developerworks/linux/library/l-sed2/index.html –  hbdgaf Mar 16 '12 at 10:56
add comment

1 Answer

up vote 2 down vote accepted

One way:

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

Explanation:

s/regex/replacement/flags
[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
add comment

Your Answer

 
discard

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.