2

how do I write the following sed command so that I can use the line breaks and code structure when I write the sed command as well as output the same code structure? As you can see I'm trying to modify CSS without making it one line.

sed -i 's/#para3{ 
font-size:21pt;    
position:absolute; 
width:500px; 
left:15px;
top:445px; 
text-align:left;
  }/#para3{ 
font-size:testpt;    
position:absolute; 
width:500px; 
left:15px;
top:445px; 
text-align:left;
  }/g' $file0

2 Answers 2

2

If you just want to replace font-size value between #para3 { and next }:

sed '/#para3 *{/,/}/{s/\(font-size: *\)21pt;/\1testpt;/}' file

To replace all block, you can try the c(change) command:

sed '/#para3 *{/,/}/c \
#para3 { \
  font-size: 3em; \
  width: 100%; \
}' file

NB: after the c command, all lines except last one must ends with a newline character (\)

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

2 Comments

This is a great answer and I'll definitely use it but I want to replace everything between #para3{ and next }
why does the 1 get replaced ?1testpt; is replaced with testpt;
0

Why don't you proceed in the following way:

  • Store the content of your css replacement patterns in 2 files (the part to be replaced and the part that will be used for the replacement operation)
  • Use the cat command to get the files content and store it in 2 variables var1="$(cat toReplace.css)"; var2="$(cat replacement.css)"
  • Use the 2 variables when calling the sed command: sed -i.bak "s/$var1/$var2/g" $file0
  • Since you are using the in-place replacement it might be better to take a backup of your file!
  • Last but not least, you will have to escape all the characters that might be interpreted as special character in your regex as {} you just have to add a backslash before them.

1 Comment

I still get the following error sed: -e expression #1, char 11: unterminated `s' command

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.