Shell版unl2xls
时间:2006-10-13 来源:enjoy48
学习perl过程中开始退回来重新系统的学习unix基本知识,此程序为在看"sed & awk"过程中根据C语言版改写,希望有人能用得到。
#unl2xls:convert unl files to xls files
#write by huangtao
UNLToXls() {
awk '
BEGIN {
FS="|"
print "<HTML>"
print "<HEAD>"
#确保中文能正常显示
print "<META http-equiv=Content-Type content=\"text/html;charset=gb2312\">"
print "</HEAD>"
print "<BODY>"
print "<TABLE border=\"1\">"
}
{
for(n=1;n<=NF-1;n++) {
if (n==1) print "<TR>"
#将0开头字符串强制设置为文本格式
if ($n ~ /^0[0-9]+$/) printf("<TD nowrap style=mso-number-format:\"\\@\">%s</TD>\n",$n)
else printf("<TD nowrap>%s</TD>\n",$n)
if (n==NF-1) printf("</TR>\n")
}
}
END {
print "</TABLE>"
print "</BODY>"
print "</HTML>"
}
' $OldFileName
rm $OldFileName
}
if [ -z "$1" ];then
echo "Usage: $0 file1.unl [file2.unl]..."
exit
fi
for OldFileName in $* ; do
if [ -s $OldFileName ];then
NewFileName=${OldFileName%unl}xls
UNLToXls $OldFileName >$NewFileName
else rm $OldFileName
fi
done
#tar cf `date +"%Y%m%d%H%M"`.rar *.xls && rm *.xls
#write by huangtao
UNLToXls() {
awk '
BEGIN {
FS="|"
print "<HTML>"
print "<HEAD>"
#确保中文能正常显示
print "<META http-equiv=Content-Type content=\"text/html;charset=gb2312\">"
print "</HEAD>"
print "<BODY>"
print "<TABLE border=\"1\">"
}
{
for(n=1;n<=NF-1;n++) {
if (n==1) print "<TR>"
#将0开头字符串强制设置为文本格式
if ($n ~ /^0[0-9]+$/) printf("<TD nowrap style=mso-number-format:\"\\@\">%s</TD>\n",$n)
else printf("<TD nowrap>%s</TD>\n",$n)
if (n==NF-1) printf("</TR>\n")
}
}
END {
print "</TABLE>"
print "</BODY>"
print "</HTML>"
}
' $OldFileName
rm $OldFileName
}
if [ -z "$1" ];then
echo "Usage: $0 file1.unl [file2.unl]..."
exit
fi
for OldFileName in $* ; do
if [ -s $OldFileName ];then
NewFileName=${OldFileName%unl}xls
UNLToXls $OldFileName >$NewFileName
else rm $OldFileName
fi
done
#tar cf `date +"%Y%m%d%H%M"`.rar *.xls && rm *.xls
相关阅读 更多 +