
On Wed, Feb 24, 2010 at 11:24 AM, albert kamau <kamau.albert@gmail.com> wrote:
author.txt
JRtolkien - lord of the rings
<?xml version="1.0" encoding="iso-8859-1"?> <library> <author name="JRTolkien" book="Lord of the rings " /> </library>
If you running linux ... or you have cygwin and sed installed on windows -- the following command will give you your desired output -- echo '<?xml version="1.0" encoding="iso-8859-1"?><library>' $(cat author.txt | sed 's/[ ]*-/-/g' | sed 's/-[ ]*/-/g' | sed 's/.*/<author name="&/' | sed 's/.*/&"\/>/' | sed 's/-/" book="/') "</library>" > author.xml it works by piping outputs - and changing outputs at each step ... first we output the header - <?xml version="1.0" encoding="iso-8859-1"?><library> then we output the text file author.txt - cat author.txt prev output is piped to - sed 's/[ ]*-/-/g' | sed 's/-[ ]*/-/g' - which trims the white space around the "-" separator prev output is piped to prefix the starting xml tag '<author name="' before the string prev output is piped to suffix the ending xml tag ' " /> ' prev output is piped to replace the "-" character with the closing of the 1st attrribute the attribute name of the 2nd attribute... ' " book-" ' to get the final ouput