0

I am trying to write a script that does certain operation on below text present in a file say myfile.txt

<MY_TEXT="XYZ" PATH="MNO"
<location= "XYZ" path="ABC" 
\location>
<R_DATA = MNOP
 <Mylocation ="ghdf" stime=20150301 etime=20150401 >
 <Mylocation ="ghdf" stime=20150401 etime=20150501 >
\R_DATA>
<Blah>
\MY_TEXT>

<MY_TEXT="ABC" PATH="EFG"
<location= "QQQ" path="LLL" 
\location>
<R_DATA = MNOP
 <Mylocation ="ghdf" stime=20150301 etime=20150401 >
 <Mylocation ="ghdf" stime=20150401 etime=20150501 >
\R_DATA>
<Blah>
\MY_TEXT>

Here i see that I have a line <MY_TEXT="XYZ" and then on this match i try and change the lines that lies between <MY_TEXT \MY_TEXT> . I need to make changes to line <Mylocation line and modify etime=20150401 to say and then append a new line after it. Now all this needs to be done in a file that i have passed or create a new file with above modification. I am looking for a way to this and its getting complicated by reading individual lines and keeping track of line.

3 Answers 3

1

You can do this with two commands through sed

sed -e '/MY_TEXT="XYZ"/,/MY_TEXT/!d' data.dat

This will watch the correct MY_TEXT block. The part that will replace the correct etime value can be done as follows

sed 's/etime=20150401/etime=$newtime/g'

You can combine these two statements in one command as follows:

sed -e '/MY_TEXT="XYZ"/,/MY_TEXT/!d;s/etime=20150401/etime=$newtime/g' data.dat

If you want the changes to be applied to the same file you can use sed's -i in-place operation flag

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

Comments

1
from=20150401   # value to match
to=20150402     # new value

sed '
    # For lines between the two markers
    /<MY_TEXT/,/\\MY_TEXT/ {
        # only for lines matching "Mylocation"
        /Mylocation/ {
            # change the "etime" value
            s/etime='"$from"'/etime='"$to"'/
            # add a newline at the beginning of the line
            ta  # ONLY if a replacement was made, goto a
            bb  # goto b
            :a
            # insert a blank line
            i
            :b
        }
    }
' file

outputs

<MY_TEXT="XYZ" PATH="MNO"
<location= "XYZ" path="ABC" 
\location>
<R_DATA = MNOP

 <Mylocation ="ghdf" stime=20150301 etime=20150402 >
 <Mylocation ="ghdf" stime=20150401 etime=20150501 >
\R_DATA>
<Blah>
\MY_TEXT>

<MY_TEXT="ABC" PATH="EFG"
<location= "QQQ" path="LLL" 
\location>
<R_DATA = MNOP

 <Mylocation ="ghdf" stime=20150301 etime=20150402 >
 <Mylocation ="ghdf" stime=20150401 etime=20150501 >
\R_DATA>
<Blah>
\MY_TEXT>

Too bad your data is not actually XML. There would be more robust methods then.

Comments

1

You say you want to modify the lines containing <Mylocation in your target block but you don't say what you want them to be changed to so I've elected to change your lines to "here is a fluffy bunny":

$ cat tst.awk         
BEGIN { RS=""; ORS="\n\n" }
/^<MY_TEXT="XYZ"/ {
    gsub(/<Mylocation[^\n]*\n/,"here is a fluffy bunny\nand an added line after it\n")
}
{ print }

$ awk -f tst.awk file
<MY_TEXT="XYZ" PATH="MNO"
<location= "XYZ" path="ABC" 
\location>
<R_DATA = MNOP
 here is a fluffy bunny
and an added line after it
 here is a fluffy bunny
and an added line after it
\R_DATA>
<Blah>
\MY_TEXT>

<MY_TEXT="ABC" PATH="EFG"
<location= "QQQ" path="LLL" 
\location>
<R_DATA = MNOP
 <Mylocation ="ghdf" stime=20150301 etime=20150401 >
 <Mylocation ="ghdf" stime=20150401 etime=20150501 >
\R_DATA>
<Blah>
\MY_TEXT>

If that's not what you want and you can't figure out how to adapt the above to do it, edit your question to provide the expected output to match your posted sample input.

2 Comments

This is not what i wanted . I wanted to modify second or say nth instance of <Mylocation , etime and then add a new line after that .
Then update your question to clarify your requirements and show the expected output given your posted input.

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.