linux下批量修改檔案中的字元

myownstars發表於2011-09-07

以下所有檔案都包含字母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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章