How to globally replace strings in lines NOT starting with a certain pattern

前端 未结 3 1868
庸人自扰
庸人自扰 2020-12-23 17:13

I want to globally replace the string foo with the string bar, using sed. This should only be done for lines which do NOT start with the string ##Input.

I can\'t get

相关标签:
3条回答
  • 2020-12-23 17:46

    You got to escape # as in \#.

    0 讨论(0)
  • 2020-12-23 17:53

    An ugly answer for an ugly request (i.e. they get what they asked for):

    echo \{
    for file in *.json; do
        sed -n '/^[\{\}]/! s/\([^\,]\)$/\1,/; /^[\{\}]/!p' $file
    done
    echo \{
    
    0 讨论(0)
  • 2020-12-23 17:55

    You just need to negate the match using !:

    sed -i '/^##Input/! s/foo/bar/g' myfile
    
    0 讨论(0)
提交回复
热议问题