#!/bin/sh
FILE=$HOME/lynx_bookmarks.html
grep -v LI $FILE > $FILE.tmp 
grep LI $FILE | awk 'BEGIN {FS=">"}{OFS=">";print $3, $1, $2,""}' | sort -df | \
awk 'BEGIN {FS=">"}{OFS=">";print $2, $3, $1,""}' | sed 's/"//g' > $FILE.tmp1
for a in `awk '{print $NF}' $FILE.tmp1 | sed 's/<\/a>//g'`
do
	if [ `grep $a $FILE.tmp1 | awk '{print length($(NF-1))}'` -eq 1 ]; then
		grep $a $FILE.tmp1 | awk '{ORS=" "; for (i=1;i<NF-1;i++) \
		print $i; print $NF; ORS="\n";print ""}' >> $FILE.tmp
	else
		grep $a $FILE.tmp1 >> $FILE.tmp
	fi
done
mv $FILE.tmp $FILE
chmod 644 $FILE $HOME/bin/long
rm $FILE.tmp1
grep LI $FILE | wc -l
