Jump to content

Need Help with a Bash Script (Text to HTML to XML)

Hi, so I'm working on a bash script to convert several .txt files in a directory into .xml. I found it was pretty tricky to accomplish that directly so i attempted .txt to .html then .html to .xml and it worked perfectly. Now, I'm still kinda new to the world of bash scripts and was wondering how I might be able to accomplish some things. The first thing is, how do i set a variable equal to the number of .txt files in a directory. Second, well, here's my pseudo code...

 

x = [number of .txt files in directory]

for i in range (x):
	txt2html [file number i].txt -o [file of same name].html

for i in range (x):
	tidy -q -asxml [file number i].html >[file of same name].xml

rm *.html

yea, i'm not exactly sure how to do for loops, or how to convert the files in a sequence and have the output file have the same name as the original, just in a new format. Any help is appreciated!

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, sgzUk74r3T3BCGmRJ said:

Why not just iterate over the files in a one-liner?


$ for i in *.txt; do txt2html $i | tidy -q -asxml > ${i%.txt}.xml; done

You're welcome to cleave that up into multiple lines and store it in an .sh file if you like, but the same idea would hold. You should read the documentation on parameter expansion and looping constructs, though the fact you're taking a round-trip through HTML makes me think you probably have some other issue further up the build chain that could be solved by reading documentation or fixing some busted code.

 

I'm not familiar with the tools you're using but I'd bet that they can be made to read and write stdin/out so don't bother making intermediate files.

thank you so much! My reasoning for going through html was because I couldn't find any tools to go straight from txt to xml. I will definately give the documentation a look!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×