0

Let say I have the following phone number in a .txt file

(682) 543-7890 

I need three sed commands to change this number.

  1. Need the number in-between parentheses (can be any number) to replace with 800
    Example text:
(800) 543-7890
  1. next I need to remove the parentheses.
    Example text:
800 543-7890
  1. For the last part I need a dash to be added in the white space between the area code and the rest of the number.
    Example text:
800-543-7890

I tried sed -e 's/([^()]*)//g' so far and it removes the parentheses and everything inside.

2
  • Why does it have to be sed and why can't it be done in one command instead of three? Also obligatory "what have you tried so far?"
    – jesse_b
    Feb 10 at 20:19
  • It's part of an assignment and needs to be a .sed file of multiple commands. if it possible to be done in one so be it. I tried sed -e 's/([^()]*)//g' so far (I'm not the smartest working with sed) and it removes the parentheses and everything inside.
    – Zed-4
    Feb 10 at 20:25
2

Single sed invocation is enough:

sed 's/(.*) */800-/'
0
1

Your command sed -e 's/([^()]*)//g' could almost work, just have to do a few adjustments:

# You don't need greedy command 'g'
# you have to account for the space
# you have to make the substitution
$ sed -e 's/([^()]*) */800-/'
800-543-7890

Of course as demonstrated by the accepted answer, there are simpler ways to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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