-2

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.

MTU="1500"

--- 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
aenagy
  • 1
  • 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. https://unix.stackexchange.com/questions/209971/is-there-any-way-to-print-value-inside-variable-inside-single-quote 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

1 Answers1

0

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.

RalfFriedl
  • 8,981
  • 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 07:32