linux下批量修改文件中的字符

以下所有文件都包含字母justin,现在只要修改*.log,将其中的justin替换为myownstars
[oracle@db oracle]$ ls
2010091008.log  2010091217.log  2010091220.log  2010091223.log  2010091302.log  2010091305.log  2010091308.log  2010091311.log  2010091314.log      crontab_rk.log   sqlldr_track_info_filter.sh
2010091009.log  2010091218.log  2010091221.log  2010091300.log  2010091303.log  2010091306.log  2010091309.log  2010091312.log  control.ctl         date.sh          sqlldr_track_info.sh
2010091216.log  2010091219.log  2010091222.log  2010091301.log  2010091304.log  2010091307.log  2010091310.log  2010091313.log  control_filter.ctl  source_file.bad
1、
使用perl命令
[oracle@db oracle]$ perl -p -i -e "s/justin/myownstars/g" /home/oracle/*.log
替换后检验,发现后缀为log的文件都没有了justin字符
[oracle@db oracle]$ find . -name '*' -print | xargs grep "justin"
./sqlldr_track_info.sh:folder=/home/oracle/justin/
./control.ctl:infile '/home/oracle/justin/source_file.csv'
./control.ctl:append into table temp_track_info_justin
./sqlldr_track_info_filter.sh:folder=/home/oracle/justin/
./control_filter.ctl:infile '/home/oracle/justin/source_file.csv'
./control_filter.ctl:append into table temp_track_info_justin

2、
使用sed
[oracle@db oracle]$ sed -i "s/justin/myownstars/g" `ls *.log`
[oracle@db oracle]$ find . -name '*' -print | xargs grep "justin"
./sqlldr_track_info.sh:folder=/home/oracle/justin/
./control.ctl:infile '/home/oracle/justin/source_file.csv'
./control.ctl:append into table temp_track_info_justin
./sqlldr_track_info_filter.sh:folder=/home/oracle/justin/
./control_filter.ctl:infile '/home/oracle/justin/source_file.csv'
./control_filter.ctl:append into table temp_track_info_justin
效果等同于1

以上都是替换单个单词,当替换路径时候,可以使用以下方式
perl -pi -e 's|/data/oracle/product|/home/oracle/product|g' /home/oracle/*
即可将/home/oracle下所有文件中的/data/oracle/product替换为/home/oracle/product

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15480802/viewspace-706799/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15480802/viewspace-706799/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值