This question already has an answer here:

I am posting this question because I am unable to add a comment to an answer submitted by Kusalananda here: "key = value" lines: how to replace a specific key's value?

He proposed the following solution:

sed -E 's/^(power[[:blank:]]*=[[:blank:]]*).*/\1something/' TheFile

Q1: What is the meaning of "\1"?

Q2: How can I modify this when dealing with key-value pairs with quotations around the value? e.g.


--- edit ---

Q3: In trying the suggestion from RalfFriedl I discovered that the name of my variable is being inserted, not the value.

sed -E 's/^(MTU[[:blank:]]*=[[:blank:]]*).*/\1$NewMtu/' MyEthFile

marked as duplicate by don_crissti, JdeBP, Thomas Dickey, Kusalananda sed Sep 14 '18 at 18:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I don't disagree that this is a duplicate question. My first problem was not being able to add a comment to the original solution I reference. – aenagy Sep 14 '18 at 18:05
  • I have updated that answer to include what \1 refers to. For your second question, it is unclear what the issue is. To insert "150", use that string in place of something. – Kusalananda Sep 14 '18 at 18:11
  • For your third question (not more than one question per question, please, and no follow-up question in the same question either, unless it's trivial), see e.g. unix.stackexchange.com/questions/209971/… or any other question relating to expanding variables inside single quotes (which the shell does not do). – Kusalananda Sep 14 '18 at 18:30
  • Don't wrap "MTU...=" in parenthesis; the parenthesis captures what you want in \1, so start after the equals sign. – Jeff Schaller Sep 14 '18 at 18:55

The meaning of \1 is that is inserts the value of the first parenthesis, in this case everything between (power[[:blank:]]*=[[:blank:]]*)

For your second question, you could use a pattern of MTU="(.*)" and replace it with \1 to get 1500.

  • I might suggest ([^"]*) so that you don't accidentally grab more than desired – Jeff Schaller Sep 14 '18 at 18:01
  • RalfFriedl: It's not clear to me how you proposed answer for Q2 would deal with whitespace around the equal sign. That is the advantage of the form used by Kusalananda. – aenagy Sep 14 '18 at 18:10
  • @aenagy It would not, because your question didn't mention white space. It seems you already know enough about regular expressions, just not about using the replacement reference. – RalfFriedl Sep 15 '18 at 7:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.