sed: Replace part of a line

后端 未结 7 2080
渐次进展
渐次进展 2020-12-05 09:53

How can one replace a part of a line with sed?

The line

DBSERVERNAME     xxx

should be replaced to:

DBSERVERNAME           


        
相关标签:
7条回答
  • 2020-12-05 09:58

    Try this

    sed -re 's/DBSERVERNAME[ \t]*([^\S]+)/\yyy/ig' temp.txt

    or this

    awk '{if($1=="DBSERVERNAME") $2 ="YYY"} {print $0;}' temp.txt

    0 讨论(0)
  • 2020-12-05 10:00

    Others have already mentioned the escaping of parentheses, but why do you need a back reference at all, if the first part of the line is constant?

    You could simply do

    sed -e 's/dbservername.*$/dbservername yyy/g'
    
    0 讨论(0)
  • 2020-12-05 10:01

    This works:

    sed -rne 's/(dbservername)\s+\w+/\1 yyy/gip'
    

    (When you use the -r option, you don't have to escape the parens.)

    Bit of explanation:

    • -r is extended regular expressions - makes a difference to how the regex is written.
    • -n does not print unless specified - sed prints by default otherwise,
    • -e means what follows it is an expression. Let's break the expression down:
      • s/// is the command for search-replace, and what's between the first pair is the regex to match, and the second pair the replacement,
      • gip, which follows the search replace command; g means global, i.e., every match instead of just the first will be replaced in a line; i is case-insensitivity; p means print when done (remember the -n flag from earlier!),
      • The brackets represent a match part, which will come up later. So dbservername is the first match part,
      • \s is whitespace, + means one or more (vs *, zero or more) occurrences,
      • \w is a word, that is any letter, digit or underscore,
      • \1 is a special expression for GNU sed that prints the first bracketed match in the accompanying search.
    0 讨论(0)
  • 2020-12-05 10:06

    You're escaping your ( and ). I'm pretty sure you don't need to do that. Try:

    sed -rne 's/(dbservername)[[:blank:]]+\([[:alpha:]]+\)/\1 yyy/gip'
    
    0 讨论(0)
  • 2020-12-05 10:11

    This might work for you:

    echo "DBSERVERNAME     xxx" | sed 's/\S*$/yyy/'
    DBSERVERNAME     yyy
    
    0 讨论(0)
  • 2020-12-05 10:17

    You shouldn't be escaping your parens. Try:

    echo "DBSERVERNAME    xxx" | sed -rne 's/(dbservername)[[:blank:]]+([[:alpha:]]+)/\1 yyy/gip'
    
    0 讨论(0)
提交回复
热议问题