How to replace one character inside parentheses keep everything else as is

后端 未结 5 1128
北恋
北恋 2021-01-19 06:48

The data looks like this :

There is stuff here (word, word number phrases)
(word number anything, word phrases), even more
...

There is a l

5条回答
  •  逝去的感伤
    2021-01-19 07:42

    Here's a version for awk that uses the parentheses as record separators:

    awk -v RS='[()]' 'NR%2 == 0 {sub(/,/,":")} {printf "%s%s", $0, RT}' file
    

    The stuff between parentheses will be every even-numbered record. The RT variable holds the character that matched the RS pattern for this record.

    Note that this only replace the first comma of the parenthesized text. If you want to replace all, use gsub in place of sub

提交回复
热议问题