Jaur
Jaur

Reputation: 85

Change value with sed/awk using value that changes

Basically i need to create multiple files containing one line and substitute that line with value which i get from loop. Maybe my little code will explain better then i am.

#!/bin/bash
DATE=$(date +%F -d 2014-06-01)
for echo in {1..5}
do
echo "<TAG></TAG>" > output$echo.xml
done
for i in {1..5}
    do
NEXT_DATE=$(date +%F -d "$DATE + $i month")
SET=$(date +%F -d "$NEXT_DATE + $i month")
sed -i "s|<TAG></TAG>|<TAG>$SET{1..5}</TAG>|" output{1..5}.xml
done

It generates me output1.xml, output2.xml ... I just cant figure out how to add that generated date valu between tags after running that script. Desired output would be:

output1.xml contains:

"<TAG>2014-08-01</TAG>"

output2.xml contains:

"<TAG>2014-10-01</TAG>"

and so on. Right now it substitutes it with generated date value + {1..5}, like so:

<TAG>2014-08-01{1..5}</TAG>

How can i achive that?

Upvotes: 1

Views: 30

Answers (1)

Barmar
Barmar

Reputation: 780714

There's no need for two loops or awk. Just put the date into the file in the first loop:

DATE=$(date +%F -d 2014-06-01)
for i in {1..5}
do
    NEXT_DATE=$(date +%F -d "$DATE + $i month")
    SET=$(date +%F -d "$NEXT_DATE + $i month")
    echo "<TAG>$SET</TAG>" > output$i.xml
done

Upvotes: 2

Related Questions