0

I would need to edit a param value, in a specific block. Can you help with the sed command in this. I know to do this with a perl script, but i need in one single command

My file looks like this

Block 1
   Param1=val1
   Param2=val2
   .
   .

Block 2
   Param1=val1
   Param2=val2
   .
   .

Block 3
.
.
.
2
  • What is your expect output ? Commented Nov 6, 2015 at 9:39
  • Replace parameter value like Param1=modifiedval1 Commented Nov 6, 2015 at 9:48

2 Answers 2

1

You can make use of sed's address: /pattern1/,/pattern2/ to handle only one "block". For example, if you just want to change Param1 in Block 1:

sed -r '/^Block 1/,/^Block 2/s/(\s*Param1=).*/\1NEW/' file

If you have many blocks, you can use ^Block 1$ or ^Block 1\s*$. This won't get mess with for example: Block 120

Sign up to request clarification or add additional context in comments.

2 Comments

Kent, I need to modify the parameter value in one of the blocks only. I tried this sed -r '/^Block 1/s/(\sParam1=.)/\1Param1=newvalue/' file. It did not edit. Am I missing anything
@cms my cmd will output the result (with param1 changed). If you want to change the file, add -i option to your gnu sed.
0

This might work for you (GNU sed):

sed -ri '/^Block/h;G;s/(Param1=)\S+(.*\nBlock 2)$/\1newval\2/;P;d' file

Store the current block in the hold space and append the block variable to each line of the file. Substitute a new parameter value if there is a match on the parameter and the block variable. Print every line and delete the block variable.

N.B. the solution above replaces parameter 1 in block 2, tailor the solution for your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.