Parsing of parenthesis with sed using regex

前端 未结 3 1931
我寻月下人不归
我寻月下人不归 2021-01-14 04:25

I am looking for a command in sed which transforms this input stream:

dummy
(key1)
(key2)dummy(key3)
dummy(key4)dummy
dummy(key5)dummy))))dummy
         


        
3条回答
  •  自闭症患者
    2021-01-14 05:10

    You can use this lookbehind based regex in grep -oP:

    grep -oP '(?<=\()[^)]+' file
    key1
    key2
    key3
    key4
    key5
    key6
    key7
    

    Or using awk:

    awk -F '[()]' 'NF>1{for(i=2; i<=NF; i+=2) if ($i) print $i}' file
    key1
    key2
    key3
    key4
    key5
    key6
    key7
    

提交回复
热议问题