程序一:
彩色显示文件程序,每个字符的颜色都不一样,随机变化,让你看花眼睛^_^:
程序代码:
#! /usr/bin/awk
# Write by dbcat
# EMail:deeperbluecat@Gmail.com
# run : awk -f ColorCat.awk YourFile
BEGIN{
srand()
}
{
split($0,Myth,"")
ColorPrint(Myth,length($0))
}
function ColorPrint(Myth,xlen)
{
for(i=1;i<=xlen;i++)
{
Color="\033[1;"int(31+7*rand())
printf "%s;3m%s\033[0m",Color,Myth[i]
}
printf "\n"
}
程序二:
彩色作图程序,有点像Gnuplot,可以作出圆形,正弦图,抛物线等等。
例如:
圆: awk 'BEGIN{while(k<10){print sin(k),cos(k);k=k+0.01}}' | awk -f ColorPlot.awk
正弦线: awk 'BEGIN{while(k<10){print sin(k),k;k=k+0.01}}' | awk -f ColorPlot.awk
抛物线: awk 'BEGIN{k=-10;while(k<10){print k^2,k;k=k+0.01}}' | awk -f ColorPlot.awk
直线: paste <(seq 1 0.01 10) <(seq 1 0.01 10) | awk -f ColorPlot.awk
如果你有想象力的话还可以作出很多意想不到的图形,比如:
圆盘: awk 'BEGIN{while(k<100){print sin(k),rand()*cos(k);k=k+0.01}}' |awk -f ColorPlot.awk
花圈: awk 'BEGIN{srand()
while(k++<20000){
x=2-3*rand()
y=2-4*rand()
if(x^2+y^2>0.6&&x^2+y^2<1||x^2+y^2<0.3&&x^2+y^2>0.1)
print x,y
}
}' | awk -f ColorPlot.awk
菱圈: awk 'BEGIN{srand()
while(k++<20000){
x=1-2*rand()
y=1-2*rand()
if(x+y<=1&&x-y<=1&&-x+y<=1&&-x-y<=1&&x^2+y^2>=1/2)
print x,y
}
}' | awk -f ColorPlot.awk
程序代码:
#! /usr/bin/awk
# GAWK彩色作图程序
# 作者: dbcat
# Email: deeperbluecat@Gmail.Com
# 日期: 2006-9-25
# 测试环境: Gawk 3.1.4, bash 3.00.16(1), SUSE 9.3
# 运行方法: awk 'BEGIN{while(k<10){print sin(k),cos(k);k=k+0.01}}' >datafile
# awk -f ColorPlot.awk datafile
BEGIN{
srand()
xlen=35
ylen=35
InitGraph(Myth,xlen,ylen)
}
{
X_Max=X_Max>$1?X_Max:$1
X_Min=X_Min<$1?X_Min:$1
Y_Max=Y_Max>$2?Y_Max:$2
Y_Min=Y_Min<$2?Y_Min:$2
X_Label[NR]=$1
Y_Label[NR]=$2
}
END{
CreateGraph(Myth,NR)
PrintGraph(Myth)
}
function InitGraph(Myth,xlen,ylen,i,j)
{
for(i=1;i<=xlen;i++)
for(j=1;j<=ylen;j++)
Myth[i,j]=" "
}
function CreateGraph(Myth,Len,i)
{
for(i=1;i<=Len;i++)
{
X_Label[i]=int((X_Label[i]-X_Min)/(X_Max-X_Min)*(xlen-1) + 1)
Y_Label[i]=int((Y_Label[i]-Y_Min)/(Y_Max-Y_Min)*(ylen-1) + 1)
Myth[X_Label[i],Y_Label[i]]=int(40+60*rand())
}
}
function PrintGraph(Myth,i,j)
{
for(i=1;i<=xlen;i++)
{
for(j=1;j<=ylen;j++)
{
color="\033[1;"int(31+7*rand())
printf " %s;1m%c\033[0m",color,Myth[i,j]
}
printf "\n"
}
}
#########################
#心形图形实例
awk 'BEGIN{while(u<20){print sin(u)*sin(v),cos(u)*sin(v+u);v=v+0.01;u=u+0.01}}' |awk -f ColorPlot.awk
#爱心型
awk 'BEGIN{while(u<20){print sin(u)*sin(v),rand()*cos(u)*sin(v+u);v=v+0.01;u=u+0.01}}'
#绳结
awk 'BEGIN{while(u<10){print sin(u+v)*sin(v),cos(u+v)*sin(v);v=v+0.01;u=u+0.01}}'
#蝴蝶
awk 'BEGIN{while(u<10){print sin(u+v)*sin(v),cos(u)*sin(v);v=v+0.01;u=u+0.01}}'
#花瓣
awk 'BEGIN{while(u<10){print sin(u+w)*cos(v)*sin(w+u),sin(u)*sin(v+u)*sin(w);v=v+0.01;u=u+0.01;w=w+0.01}}'
#蝙蝠
awk 'BEGIN{while(u<10){print sin(u+w)*cos(v+w)*sin(w+u),sin(u)*sin(v+u)*sin(w);v=v+0.01;u=u+0.01;w=w+0.01}}'
#螺旋
awk 'BEGIN{while(w<20){print sin(w)*w,cos(w)*w;w=w+0.01}}'
#花蕊
awk 'BEGIN{while(u<10){print sin(u)*cos(v+u)*sin(u+v),cos(v+u)*cos(u)*sin(v+u);v=v+0.01;u=u+0.01}}' | awk -f ColorPlot.awk
#剪刀
awk 'BEGIN{while(u<10){print sin(u+v)*cos(v+u)*sin(u+v),cos(v+u)*cos(u)*sin(v+u);v=v+0.01;u=u+0.01}}'| awk -f ColorPlot.awk
阅读(1403) | 评论(0) | 转发(0) |