1

Suppose there is one file1.txt where content is written

Ramesh
Suresh
Raman

From the below shell script ,I am reading the content name from file1 .txt and preparing the Name=$line in echo statement.(Not able to show in shell script,in line 3 of sh it is written.

while read -r line
do
   echo "<Name="$line"/>"
done <"file.txt"

There is another xml file2.xml

<project>
<target>
start
end
</target>
</project>

I want to enhance my shell script which will insert lines to file2.xml. If in file2.xml, we find pattern end, then insert the content above end , each in new line. Can anyone please help in modifying the shell script.

Output should look like:-

<project>
<target>
start
Name=Ramesh
Name=Suresh
Name=Raman
end
</target>
</project>

2 Answers 2

2

I would do this:

names=$(sed s/^/Name=/ file1.txt)

ed file2.txt <<END
/^end$/i
$names
.
wq
END

now:

$ cat file2.txt
<project>
<target>
start
Name=Ramesh
Name=Suresh
Name=Raman
end
</target>
</project>
1

You could use awk

awk 'NR==FNR{Lines=Lines "Name=" $0 "\n";next}/end/{print Lines $0 ;next}1' file{1,2}

<project>
<target>
start
Name=Ramesh
Name=Suresh
Name=Raman
end
</target>
</project>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .