0

I am trying to use the sed functionality of bash scripting to find and replce the content in a file. Not sure if my approach is correct or not but I dont want to go with regex.

Input:

 <if property="headless">
        <VMArg name="-Djava.awt.headless=true"/>
    </if>

    <VMArg name="-Dfile.encoding=UTF-8"/>
    <VMArg name="-Djava.util.logging.config.file=$VDISTDIR/system/conf/logging.properties"/>
    <VMArg name="-Dlog4j.configurationFile=file:///$VDISTDIR/system/conf/log4j2.yaml"/>

Expected:

 <if property="headless">
        <VMArg name="-Djava.awt.headless=true"/>
    </if>
This is the replaced text
    <VMArg name="-Dfile.encoding=UTF-8"/>
    <VMArg name="-Djava.util.logging.config.file=$VDISTDIR/system/conf/logging.properties"/>
    <VMArg name="-Dlog4j.configurationFile=file:///$VDISTDIR/system/conf/log4j2.yaml"/>

Trying to develop something like below but unfortunately its not working.

#!/bin/bash
find='<if property="headless">
        <VMArg name="-Djava.awt.headless=true"/>
    </if>

    <VMArg name="-Dfile.encoding=UTF-8"/>'

replace='<if property="headless">
        <VMArg name="-Djava.awt.headless=true"/>
    </if>

    <VMArg name="-Dfile.encoding=UTF-8"/>'

sed "s/$find/$replace/g" filename.input

Error:

sed: -e expression #1, char 7: unterminated `s' command
2
  • Please edit your question and explain what exactly means "unfortunately its not working". Why is there a \ before #!/bin/bash? Commented Oct 27, 2022 at 14:00
  • If the question is "replace empty row with text" this might work awk 'NF==0{$0 = "This is the replaced text"}1' file Commented Oct 27, 2022 at 14:01

1 Answer 1

2

The answer is: sed -i -e '1h;2,$H;$!d;g' -Ee 's/(<VMArg name="-Djava.awt.headless=true"\/>\n\s+<\/if>)/\1\nThis Is replaced text/' file

Working solution:

$ cat file
<if property="headless">
        <VMArg name="-Djava.awt.headless=true"/>
    </if>

    <VMArg name="-Dfile.encoding=UTF-8"/>
    <VMArg name="-Djava.util.logging.config.file=$VDISTDIR/system/conf/logging.properties"/>
    <VMArg name="-Dlog4j.configurationFile=file:///$VDISTDIR/system/conf/log4j2.yaml"/>

$ cat file  | sed -e '1h;2,$H;$!d;g' -Ee 's/(<VMArg name="-Djava.awt.headless=true"\/>\n\s+<\/if>)/\1\nThis Is replaced text/'
<if property="headless">
        <VMArg name="-Djava.awt.headless=true"/>
    </if>
This Is replaced text

    <VMArg name="-Dfile.encoding=UTF-8"/>
    <VMArg name="-Djava.util.logging.config.file=$VDISTDIR/system/conf/logging.properties"/>
    <VMArg name="-Dlog4j.configurationFile=file:///$VDISTDIR/system/conf/log4j2.yaml"/>```
Sign up to request clarification or add additional context in comments.

3 Comments

No need for the cat though, just sed -e ... file
@Jetchisel In solution, there is sed without cat, you need -i to do in place change thought. The cat is only to show solution working :)
Oh, right, I did not see that :-)

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.