免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2921 | 回复: 13
打印 上一主题 下一主题

把行插入命令模块的方法? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-06 10:45 |只看该作者 |倒序浏览
有一个一万行的文本文件,格式如下:
./s2004/1245/s222981245.shtml
./s2004/6620/s222976620.shtml
./s2004/2536/s222762536.shtml
./s2004/1491/s222981491.shtml
./s2004/1493/s222981493.shtml
./s2004/1486/s222981486.shtml
…………
它们对应着相关的一些文件

用批处理方式到这些页面中计数某个关键字,套用的命令模板:
#cat  "" * | grep -c 'sjofjos' >count.files

我想得到这样的一个一万行的文件
cat  "./s2004/1245/s222981245.shtml" * | grep -c 'sjofjos' >count.files
cat  "./s2004/6620/s222976620.shtml" * | grep -c 'sjofjos' >count.files
cat  "./s2004/2536/s222762536.shtml" * | grep -c 'sjofjos' >count.files
…………


当然只是对文件中的某关键字计数,用grep加上-r就能实现。不过我觉得上面这样的方式来处理有更多的地方能应用到。请大家指点一下

论坛徽章:
0
2 [报告]
发表于 2007-09-06 11:00 |只看该作者
没有太明白什么意思 ...是说在文件的头和尾加上一些字符么??

论坛徽章:
0
3 [报告]
发表于 2007-09-06 11:03 |只看该作者
cat  "" *  在""之间插入字符。变成如: cat  "./s2004/1245/s222981245.shtml" *

不过要批处理才行。我的目的是 读一个文件中的行。把每一行都插入到""的中间,然后输出

[ 本帖最后由 lovfreebsd 于 2007-9-6 11:07 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2007-09-06 11:06 |只看该作者
for i in `more a.txt`
do
echo "cat  "$i" * | grep -c 'sjofjos' >count.files" >>b.txt
done
这样好像也可以把

[ 本帖最后由 mxbao 于 2007-9-6 11:58 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2007-09-06 11:18 |只看该作者
谢谢你mxbao。
我觉得还是我表达的不够清楚
我是想变成 cat  "$i" * | grep -c 'sjofjos' >count.files 这样的字段保存在另外一个文件中,而不是想马上执行这个命令

我希望可以得到一个文本文件。里面包含了一行一行这样的字段
cat  "./s2004/1245/s222981245.shtml" * | grep -c 'sjofjos' >count.files
cat  "./s2004/6620/s222976620.shtml" * | grep -c 'sjofjos' >count.files
cat  "./s2004/2536/s222762536.shtml" * | grep -c 'sjofjos' >count.files
……………………

论坛徽章:
0
6 [报告]
发表于 2007-09-06 11:22 |只看该作者
sed -i 's/^\./cat  "\./g' -e 's/$/"\* \| grep -c 'sjofjos' >count.files/g' ufile ?
这样应该可以了

[ 本帖最后由 mxbao 于 2007-9-6 11:57 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2007-09-06 11:39 |只看该作者
./s2004/1245/s222981245.shtml
./s2004/6620/s222976620.shtml
./s2004/2536/s222762536.shtml
……

变成
cat  "./s2004/1245/s222981245.shtml" * | grep -c 'sjofjos' >count.files
cat  "./s2004/6620/s222976620.shtml" * | grep -c 'sjofjos' >count.files
cat  "./s2004/2536/s222762536.shtml" * | grep -c 'sjofjos' >count.files
……

[ 本帖最后由 lovfreebsd 于 2007-9-6 11:49 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2007-09-06 12:55 |只看该作者
bash-2.05b# more sp.txt
./index.html
./fansclub/index.html
./summerleague/index.html
./starszone/index.html
./womenclub/index.html
./sports_scrollnews.shtml
./photo/index.html

bash-2.05b# sed -i 's/^\./cat  "\./g' -e 's/$/"\* \| grep -c 'sjofjos' >count.files/g' sp.txt  
sed: rename(): No such file or directory

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
9 [报告]
发表于 2007-09-06 12:55 |只看该作者
awk '{print "cat \042",$0"\042 * | grep -c \047sjofjos\047 >count.files"}' urfile

论坛徽章:
0
10 [报告]
发表于 2007-09-06 13:27 |只看该作者
[root@3.7.0Biz-47 /home/aaa]# sed  's/^/cat  "/g' o |sed  's/$/"\* | grep -c 'sj
ofjos' >count.files/g'
cat  "./s2004/1245/s222981245.shtml"* | grep -c sjofjos >count.files
cat  "./s2004/6620/s222976620.shtml"* | grep -c sjofjos >count.files
cat  "./s2004/2536/s222762536.shtml"* | grep -c sjofjos >count.files
[root@3.7.0Biz-47 /home/aaa]# sed  's/^/cat  "/g' -e 's/$/"\* | grep -c 'sjofjos' >count.files/g' o
sed: can't read s/^/cat  "/g: Not a directory
./s2004/1245/s222981245.shtml"* | grep -c sjofjos >count.files
./s2004/6620/s222976620.shtml"* | grep -c sjofjos >count.files
./s2004/2536/s222762536.shtml"* | grep -c sjofjos >count.files
发现一个问题,为什么用管道可以,用-e报错呢??
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP