1

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).

2

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.
  • 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 clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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