首页 > Linux操作系统 > Linux操作系统 > 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博客 ” ,链接:https://blog.itpub.net/15480802/viewspace-706799/ ,如需转载,请注明出处,否则将追究法律责任。