Reputation: 456
I've got a problem with writing small bash script for making html list of files based on names. I've got files with a naming pattern INFO1 Bla Bla - INFO2 - INFO3.doc, and I want to have someting like this in a output:
<li id="INFO1Blabla">
<h5>INFO1 Bla bla</h5>
<p>INFO2</p>
<a href="files/INFO1 - INFO2 - INFO3.doc">download</a>
</li>
I'm trying to do this using bash + awk, but i've got some problems even when doing testing in a shell, as you see:
$ ls > list.txt
$ for i in 'cat list.txt'; do awk -F "-" '{print $2}' > list2.txt; done
And loop is probably infinitive, cause for 10min its working..
If somebody can help me, I'll be very happy cause I can't use php in this case and I need to generate this on my side using bash..
Cheers guys and take care
Upvotes: 2
Views: 2142
Reputation: 247042
for f in *.doc; do (
IFS=-
set -- $f
echo "<li><h5>$1</h5><p>$3</p><a href='files/$*'>download</a></li>"
)
done
The part in parentheses is executed in a subshell, so changes to IFS variable are localized. A test:
$ touch 'info1 - inf2 - info 3.doc'
$ touch 'First name blabla - Second part bla boa - third part, unimportant.doc'
$ for f in *.doc; do (
> IFS=-
> set -- $f
> echo "<li><h5>$1</h5><p>$3</p><a href='files/$*'>download</a></li>"
> )
> done
<li><h5>First name blabla </h5><p> third part, unimportant.doc</p><a href='files/First name blabla - Second part bla boa - third part, unimportant.doc'>download</a></li>
<li><h5>info1 </h5><p> info 3.doc</p><a href='files/info1 - inf2 - info 3.doc'>download</a></li>
Upvotes: 1
Reputation: 45670
Are you looking for this?:
$ ls -1
INFO1 - INFO2 - INFO3.doc
INFO1 - INFO4 - INFO6.doc
l.sh
$ ./l.sh
<li>
<h5>INFO1</h5>
<p>INFO2</p>
</li>
<li>
<h5>INFO1</h5>
<p>INFO4</p>
</li>
The script:
#!/bin/bash
for i in *.doc
do
# split file-name into array
IFS='- ' read -a array <<< "$i"
echo "<li>"
echo "<h5>${array[0]}</h5>"
echo "<p>${array[1]}</p>"
echo "</li>"
done
update
If filename looks like this:
First name blabla - Second part bla boa - third part, unimportant.doc
Change the IFS-parameter to a single -
instead.
Make this change:
IFS='-' read -a array <<< "$i"
output:
$ ./l.sh
<li>
<h5>First name blabla </h5>
<p> Second part bla boa </p>
</li>
<li>
<h5>INFO1 </h5>
<p> INFO2 </p>
</li>
<li>
<h5>INFO1 </h5>
<p> INFO4 </p>
</li>
update 2
#!/bin/bash
for i in *.doc
do
# split file-name into array
IFS='-' read -a array <<< "$i"
echo "<li>"
echo "<h5>${array[0]}</h5>"
echo "<p>${array[1]}</p>"
echo "</li>"
echo "<a href=\"files/${i}\">download</a>"
done
Output:
<li>
<h5>First name blabla </h5>
<p> Second part bla boa </p>
</li>
<a href="files/First name blabla - Second part bla boa - third part, unimportant.doc">download</a>
<li>
<h5>INFO1 </h5>
<p> INFO2 </p>
</li>
<a href="files/INFO1 - INFO2 - INFO3.doc">download</a>
<li>
<h5>INFO1 </h5>
<p> INFO4 </p>
</li>
<a href="files/INFO1 - INFO4 - INFO6.doc">download</a>
Upvotes: 3