1

I have a file, in which I have to find and change strings by specific pattern (phone number). The regex is:

^\+[0-9]{3} \([0-9]{2}\) [0-9]{7}$

When I use it in command:

grep "^\+[0-9]{3} \([0-9]{2}\) [0-9]{7}$" -E filename

It works. But when I try to use it in sed to replace all parenthesis by spaces and add spaces in 13 and 15 position, it doesn't works and I don't have ideas why.

My variants are:

sed '/^\+[0-9]{3} \([0-9]{2}\) [0-9]{7}$/s/[()]//' filename

(only for replacing parenthesis)

sed -e '/^\+[0-9]{3} \([0-9]{2}\) [0-9]{7}$/s/[()]//' -e '/^+[0-9]{2} ([0-9]{2}) [0-9]{7}/s/./& /11;s/./& /14' filename

file structure:

    +380 44 123 45 67
    +380 (44) 1234567
    +350 (56) 1454557
    +330 (76) 1255557
    +380 44 3534 45 67
    +320 (45) 1237887
    +310 (54) 1939997
    adasd
    asdddddddddddd
    sssdad

expected output:

    +380 44 123 45 67
    +380 44 123 45 67
    +350 56 145 45 57
    +330 76 125 55 57
    +380 44 3534 45 67
    +320 45 123 78 87
    +310 54 193 99 97
    adasd
    asdddddddddddd
    sssdad
4
  • you need to add -E option to sed command as well, which you use with grep for same purpose of using extended regex Commented Oct 5, 2016 at 15:59
  • @Sundeep thanks for response! but when I write: sed '/^\+[0-9]{3} ([0-9]{2}) [0-9]{7}$/s/[()]//' -E filename it removes only opening parenthesis and I couldn't fix it( Commented Oct 5, 2016 at 16:04
  • well I didn't see if there were issues with regex as well.. would be good if you can add sample input file with expected output so that it can be tested... see stackoverflow.com/help/mcve for more details Commented Oct 5, 2016 at 16:05
  • @Sundeep I've added it! I need to find and replace numbers in format +380 (44) 1234567 to format +380 44 123 45 67 Commented Oct 5, 2016 at 16:08

3 Answers 3

1

Here's one way to do it:

$ cat ip.txt 
+380 44 123 45 67
+380 (44) 1234567
+350 (56) 1454557
+330 (76) 1255557
+380 44 3534 45 67
+320 (45) 1237887
+310 (54) 1939997
adasd
asdddddddddddd
sssdad

$ sed -E 's/^(\+[0-9]{3}) \(([0-9]{2})\) ([0-9]{3})([0-9]{2})([0-9]{2})$/\1 \2 \3 \4 \5/' ip.txt 
+380 44 123 45 67
+380 44 123 45 67
+350 56 145 45 57
+330 76 125 55 57
+380 44 3534 45 67
+320 45 123 78 87
+310 54 193 99 97
adasd
asdddddddddddd
sssdad
  • () can be used to surround a pattern so that the matched text inside them can be backreferenced in replacement section
  • \1 corresponds to first such captured group, \2 to second and so on
  • To match ( or ) themselves, we need to use escape them like \( and \)
  • So, here the numbers are captured as per required output, excluding the () present in input line so that they are not part of output
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I don't have any idea how,but it works perfectly!
@andrew_j glad to know it works, I will add an explanation and let you know :)
Thank you very much, I've got it!
0

Your sed command is wrong. My way:

sed -E 's/^\+[0-9]{3} \([0-9]{2}\) [0-9]{7}$/[()]/'

2 Comments

I have trouble, -E of couse.
output doens't meet requirements that I've mentioned in question(
0

Use:

sed -e 's|[()]||g' so-tel.txt | sed -E 's|([0-9]{3})([0-9]{2})([0-9]{2})|\1 \2 \3|'

to transform so-tel.txt:

+380 44 123 45 67
+380 (44) 1234567
+350 (56) 1454557
+330 (76) 1255557
+380 44 3534 45 67
+320 (45) 1237887
+310 (54) 1939997
adasd
asdddddddddddd
sssdad

into:

+380 44 123 45 67
+380 44 123 45 67
+350 56 145 45 57
+330 76 125 55 57
+380 44 3534 45 67
+320 45 123 78 87
+310 54 193 99 97
adasd
asdddddddddddd
sssdad

Explanation:

's|[()]||g'

substitute any ( and ) with nothing, globally

's|([0-9]{3})([0-9]{2})([0-9]{2})|\1 \2 \3|'

substitute and capture seven successive digits in lengths 3, 2, and 2, with the captured digit groups separated by a space.

3 Comments

thanks, but problem is that I also need to add spaces in some positions only in strings which fit by regex
Yes, That's what I neeed!
You can see expected output in question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.