enclose strings in html tags

B

bob2112

Guest
hi i have the following file which contains.

HOSTNAME1 randomstring1
HOSTNAME12 randomstring2

id like to change it to

<name>HOSTNAME1</name>
<string>randomstring1</string>

<name>HOSTNAME12</name>
<string>randomstring2</string>

Please help
 


Assuming that there are no spaces in your random strings, something like this should work:
Code:
cat inputfile | awk 'NF>0 {print"<name>",$1,"</name>\n<string>",$2,"</string>\n"}' > outputfile
Where inputfile is the path/filename of your input text file and outputfile is the path/filename for your desired output file.

That one-liner simply pipes your input file to awk, checks that each line contains at least one field/column (to prevent empty tags being written to the output file when empty lines are found in the file) and then adds some html tags either side of the two columns, with a newline at the end of each closing tag and writes the output to another file.

Note: The above will not work properly if you have spaces in your hostname or in your randomstring fields. I've just gone by the info you have given!

Should at least give you a start!
 
Last edited:


Top