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

后端 未结 5 1162
北恋
北恋 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:49

    Using awk

    $ awk -v FS="" -v OFS="" '{ c=0; for(i=1; i<=NF; i++){ if( $i=="(" || $i ==")" ) c=1-c; if(c==1 && $i==",") $i=":" } }1' file
    There is stuff here (word: word number phrases)
    (word number anything: word phrases), even more
    

    -v FS="" -v OFS="" Set FS to null so that each char is treated as a field.

    set variable c=0. Iterate over each field using for loop and toggle the value of c if ( or ) is encountered.
    if c==1 and , appears then replace it to :

提交回复
热议问题