Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

sed: Replace part of a line

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

The line

DBSERVERNAME     xxx

should be replaced to:

DBSERVERNAME     yyy

The value xxx can vary and there are two tabs between dbservername and the value. This name-value pair is one of many from a configuration file.

I tried with the following backreference:

echo "DBSERVERNAME    xxx" | sed -rne 's/(dbservername)[[:blank:]]+([[:alpha:]]+)/1 yyy/gip'

and that resulted in an error: invalid reference 1 on `s' command's RHS.

Whats wrong with the expression? Using GNU sed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...