免费注册 查看新帖 |

Chinaunix

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

求关键字替换的 shell脚本.... [复制链接]

论坛徽章:
0
发表于 2010-07-20 10:47 |显示全部楼层
10可用积分
shell脚本如下
#!/bin/bash
f="dic.txt"
find .  -type f -name "*.txt" | while read FILE
do
    awk 'BEGIN{ FS="=" }
    FNR==NR{ s[$1]=$2;  next }
    {
       for(i in s){
        if( $0 ~ i ){ gsub(i,s[i]) }
       }
       print $0
    }' $f $FILE  > temp
    mv temp $FILE
done

我想把脚本中 dic.txt 和 "*.txt"中的txt作为参数传给脚本,请问如何修改...

最佳答案

查看完整内容

f="dic.txt" 改成 f=$1find . -type f -name "*.txt" | while read FILE 改成find . -type f -name "*.$2" | while read FILE执行方式: ./xxx.sh dic.txt txt

论坛徽章:
0
发表于 2010-07-20 10:47 |显示全部楼层
f="dic.txt"  改成   f=$1
find .  -type f -name "*.txt" | while read FILE 改成
find .  -type f -name "*.$2" | while read FILE


执行方式: ./xxx.sh dic.txt txt

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
发表于 2010-07-20 10:56 |显示全部楼层
回复 1# kingsu


    位置参数,如:
#!/bin/bash
f=$1
find .  -type f -name "*.$2" | while read FILE

调用:
$ ./script dic.txt txt

论坛徽章:
0
发表于 2010-07-20 10:56 |显示全部楼层
  1. #!/bin/bash
  2. f="dic"
  3. find .  -type f -name "*.$1" | while read FILE
  4. do
  5.     echo "deal $FILE"
  6.     awk 'BEGIN{ FS="=" }
  7.     FNR==NR{ s[$1]=$2;  next }
  8.     {
  9.        for(i in s){
  10.         if( $0 ~ i ){ gsub(i,s[i]) }
  11.        }
  12.        print $0
  13.     }' $f.$1 $FILE
  14. #    mv temp $FILE
  15. done
复制代码
这么个意思?
使用
script.sh txt

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
发表于 2010-07-20 10:57 |显示全部楼层

  1. #!/bin/bash
  2. f="$1"
  3. find .  -type f -name "$2" | while read FILE
  4. do
  5.     awk 'BEGIN{ FS="=" }
  6.     FNR==NR{ s[$1]=$2;  next }
  7.     {
  8.        for(i in s){
  9.         if( $0 ~ i ){ gsub(i,s[i]) }
  10.        }
  11.        print $0
  12.     }' $f $FILE  > temp
  13.     mv temp $FILE
  14. done
复制代码
  1. ./test.sh dic.txt "*.txt"
复制代码

论坛徽章:
0
发表于 2010-07-20 11:01 |显示全部楼层
其实我是想要修改awk中$1 $2  的写法

awk的 $1 $2 跟shell 参数的$1 $2冲突问题

论坛徽章:
0
发表于 2010-07-20 11:05 |显示全部楼层
看见分,大家都拼命了!!!
{:3_188:}

论坛徽章:
0
发表于 2010-07-20 11:10 |显示全部楼层
哎, 下手慢了

论坛徽章:
0
发表于 2010-07-20 11:11 |显示全部楼层
回复 7# bfz814


  当然,几个月都见不到悬赏帖的  

论坛徽章:
0
发表于 2010-07-20 11:12 |显示全部楼层
回复 6# kingsu


不会冲突啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP