shellunixsedpattern-matching

Sed opening and closing parentheses contents


How to read numbers between pattern?

Input string:

([\\S\\s]{8}))([\\S\\s]{20}))([\\S\\s]{10}))

Output should be:

8
20
10

Note: opening and closed parenthesis are not fixed but it will be in pairs.


Solution

  • This might work for you (GNU sed):

    sed -E 's/^[^{]*\{([0-9]+)\}/\1\n/;/^[0-9]/P;D' file
    

    Remove all non { characters from the start of each line.

    Pattern match a { followed by digits followed by } and replace it by those digits and a newline.

    If the start of the line contains digits, print it.

    Delete upto and including the first newline.

    Repeat.