Replace only if string exists in current line

前端 未结 2 895
猫巷女王i
猫巷女王i 2021-01-30 17:28

I have a line such as:

sed -i \'s/mystring/newstring/\' $target

This command will change all mystring to newstring.

相关标签:
2条回答
  • 2021-01-30 17:33

    Solution

    Assuming your input file $target contains the following:

    some text mystring some other text
    some text mystring a searchstring
    just some more text
    

    This command:

    sed -i -e '/searchstring/ s/mystring/1/ ; /searchstring/! s/mystring/0/' $target
    

    will change its content to:

    some text 0 some other text
    some text 1 a searchstring
    just some more text
    

    Explanation

    The script contains two substitute (s) commands separated by a semicolon.

    The substitute command accepts an optional address range that select which lines the substitution should take place.

    In this case regexp address was used to select lines containing the searchstring for the first command; and the lines that do not contain the searchstring (note the exclamation mark after the regexp negating the match) for the second one.

    Edit

    This command will perform better and produce just the same result:

    sed -i -e '/searchstring/ s/mystring/1/ ; s/mystring/0/' $target
    

    The point is that commands are executed sequentially and thus if there is still a mystring substring in the current line after the first command finished then there is no searchstring in it for sure.

    Kudos to user946850.

    0 讨论(0)
  • 2021-01-30 17:47

    This is from the sed one-liners page:

    OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to large input files or slow processors or hard disks), substitution will be executed more quickly if the "find" expression is specified before giving the "s/.../.../" instruction. Thus:

    sed 's/foo/bar/g' filename         # standard replace command
    sed '/foo/ s/foo/bar/g' filename   # executes more quickly
    sed '/foo/ s//bar/g' filename      # shorthand sed syntax
    

    Speed is not the issue of the question at hand, but the syntax hints help formulating the solution:

    sed -i '/searchstring/ s/mystring/1/; s/mystring/0/' $target
    
    0 讨论(0)
提交回复
热议问题