|
biansj |
发表于: 2002/12/13 10:19pm
|
| 写的有些复杂,我也没什么好方法 #!/usr/bin/ksh awk '{print $2 }' 1.txt | sort -u > ./1.txt.tmp while read keyword do keyno=0 keyvalue=0 grep $keyword 1.txt | while read no nouse value do if [ $keyno -ge $no -o $keyno -eq 0 ] then keyno=$no fi (( keyvalue=keyvalue + value )) done echo "$keyno $keyword $keyvalue" done<1.txt.tmp > 2.txt rm -f ./1.txt.tmp
|
| |
|
flyegg |
发表于: 2002/12/13 11:39pm
|
echo "aaa `cat test.txt|awk '{if($1~/aaa/) print $2}'|awk '{kkk+=$1}END{printf "%9.2f" , kkk}'`" > test.out echo "bbb `cat test.txt|awk '{if($1~/bbb/) print $2}'|awk '{kkk+=$1}END{printf "%9.2f" , kkk}'`" >> test.out echo "ccc `cat test.txt|awk '{if($1~/ccc/) print $2}'|awk '{kkk+=$1}END{printf "%9.2f" , kkk}'`" >> test.out |
| |
|
chinawwt |
发表于: 2002/12/14 09:45am
|
| biansj,你的程序不是不行,bsh可以吗? |
| |
|
chinawwt |
发表于: 2002/12/14 09:48am
|
biansj, 一面字我打错了,程序不能运行,bsh可以吗? |
| |
|
nhrms |
发表于: 2002/12/14 09:58am
|
去掉第一行: awk '{print $2 }' 1.txt | sort -u > ./1.txt.tmpwhile read keyword do keyno=0 keyvalue=0 grep $keyword 1.txt | while read no nouse value do if [ $keyno -ge $no -o $keyno -eq 0 ] then keyno=$no fi (( keyvalue=keyvalue + value )) done echo "$keyno $keyword $keyvalue" done<1.txt.tmp > 2.txt rm -f ./1.txt.tmp |
| |
|
chinawwt |
发表于: 2002/12/14 10:50am
|
运行后提示: +:not found |
| |
|
biansj |
发表于: 2002/12/14 12:41pm
|
#!/usr/bin/bsh awk '{print $2 }' 1.txt | sort -u > ./1.txt.tmpwhile read keyword do keyno=0 keyvalue=0 grep $keyword 1.txt | while read no nouse value do if [ $keyno -ge $no -o $keyno -eq 0 ] then keyno=$no fi keyvalue=`expr $keyvalue + $value` #(( keyvalue=keyvalue + value )) done echo "$keyno $keyword $keyvalue" done<1.txt.tmp > 2.txt rm -f ./1.txt.tmp
|
| |
|
chinawwt |
发表于: 2002/12/14 03:04pm
|
运行后,输出: 0 aaa 0 0 bbb 0 0 ccc 0 不对! |
| |
|
biansj |
发表于: 2002/12/14 03:12pm
|
你把我写的内容C&P一个文件file,chmod +x file,然后运行./file 注意1.txt要在当前目录,结果为2.txt,如果不对,我就不清楚了 |
| |
|
chinawwt |
发表于: 2002/12/14 03:21pm
|
| 我刚才就是这么做的,出来结果就这样 |
| |
|
红豆 |
发表于: 2002/12/16 01:17am
|
改成这样就可以了。[code] awk '{print $2 }' test.txt | sort -u > ./1.txt.tmp while read keyword do keyno=0 keyvalue=0 grep $keyword test.txt > 2.txt.tmp while read no nouse value do if [ $keyno -ge $no -o $keyno -eq 0 ] then keyno=$no fi keyvalue=`expr $keyvalue + $value` done < 2.txt.tmp echo "$keyno $keyword $keyvalue" done <1.txt.tmp rm -f ./[12].txt.tmp [/code] |
| |
|
laoxia |
发表于: 2002/12/16 07:41am
|
环境为PC SOLARIS 7。 上边的还是执行不了,俺看一下,原来在执行 awk '{print $2 }' test.txt | sort -u > ./1.txt.tmp 时,产生的文件 1.txt.tmp 的头两行为空格,导致脚本执行不好。俺把那行改一下,变为两行: ------------------------------------------------- awk '{print $2 }' 1.txt | sort -u > ./1.txt.temp grep -v "^ *$" 1.txt.temp > 1.txt.tmp ------------------------------------------------ 执行正常: ------------------------------------------------ # ./sortnadd 0+1 aaa 3000 0+2 bbb 2000 0+5 ccc 1000 ----------------------------------------------- 哪位高手指点一下,为何有空行? |
| |
|
laoxia |
发表于: 2002/12/16 08:05am
|
| 0+1 aaa 3000 0+1 啥子意思? |
| |
|
laoxia |
发表于: 2002/12/16 08:19am
|
| 楼上贴子作废啊,SORRY,我自己把自己骗了 |
| |
|
edwardcj |
发表于: 2002/12/16 08:40am
|
楼上各位有考虑过如果原始数据量很大(比如5万条、50万条?)时执行效率的问题吗? 如果处理每个关键字都要扫描一下源文件的话似乎太慢了? #!/bin/sh sort +1 -2 test.txt > 1.txt awk 'BEGIN{no=-1;}{if (no==-1){no=$1;k=$2;tot=0;} \ if (k==$2) {tot+=$3;} \ else{printf "%d %s %d\n", no,k,tot; \ no=$1; k=$2; tot=$3;} } \ END{printf "%d %s %d\n", no,k,tot;}' 1.txt > output.txt rm -f 1.txt |
| |
|
laoxia |
发表于: 2002/12/16 09:32am
|
| 兄弟高见, sort +1 -2 啥子意思? |
| |
|
laoxia |
发表于: 2002/12/17 12:19pm
|
| 弟兄们,俺是新手,请指教则个! |
| |
|
laoxia |
发表于: 2002/12/18 04:42am
|
| up |
| |
|
红袖添香 |
发表于: 2002/12/18 05:33am
|
[url]http://www.fanqiang.com/a1/b5/20010923/0805001336.html[/url] |
| |
|
laoxia |
发表于: 2002/12/18 05:44am
|
| 感谢 |
| |
|
valentine |
发表于: 2002/12/18 10:14am
|
这个问题的本质还是分组求和问题,第一个自定相对来说无甚含义,因此我简单的给出下面的答案: awk '{total[$2]+=$3}END{for (s in total){printf("%s %d\n",s,total[s])}' 1.txt |
| |
|
nkliyong |
发表于: 2002/12/18 10:34am
|
| 斑竹,这是什么shell?ksh 好象不可以 |
| |
|
nile |
发表于: 2002/12/18 10:51am
|
[quote][b]下面引用由[u]红豆[/u]在 [i]2002/12/16 01:17am[/i] 发表的内容:[/b] 改成这样就可以了。 awk '{print $2 }' test.txt | sort -u > ./1.txt.tmp while read keyword do ... [/quote]为什么运行后 总是 0 aaa 0 0 bbb 0 0 ccc 0 :emn7: :emn8: :emn9: :emn14: :emn13: |
| |
|
valentine |
发表于: 2002/12/18 11:02am
|
[quote][b]下面引用由[u]nkliyong[/u]在 [i]2002/12/18 10:34am[/i] 发表的内容:[/b] 斑竹,这是什么shell?ksh 好象不可以 [/quote] 哪里不可以? 这是awk的问题,如果你引号用的对的化,有shell无关。 |
| |
|
|