linux 批量替換文件內容及查找某目錄下所有包含某字符串的文件


轉載請注明來源:https://www.cnblogs.com/hookjc/

1. sed

C代碼  
  1. grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'  

 

例如:

 

對象:文件夾

C代碼  
  1. grep -rl 'windows' ./path | xargs sed -i 's/windows/linux/g'  

 

2. find

對象:文件

C代碼  
  1. find -name 'test' | xargs perl -pi -e 's|windows|linux|g'  

 

 這里使用了perl語言,使用-e加上一段代碼,從而批量地將當前目錄及所有子目錄下的file.log文件中的string1替換成了string2; string支持正則表達式
 

3. awk

C代碼  
  1. grep -i "windows" -r ./path | awk -F : '{print $1}' | sort | uniq | xargs sed -i 's/windows/linux/g'  
 這里使用了shell命令,先查找出文件,再用awk分割(以:切分),再行替換!
注意:   grep可以使用正則,也可以使用\轉義一些特殊字符,比如“等  
sed -i 's/\"localhost\"/\"10.2.2.2\"/g' /home/my.conf
更多:

 

sed replace word / string syntax

The syntax is as follows:

C代碼  
  1. sed -i 's/old-word/new-word/g' *.txt  

 

GNU sed command can edit files in place (makes backup if extension supplied) using the -i option. If you are using an old UNIX sed command version try the following syntax:

C代碼  
  1. sed 's/old/new/g' input.txt > output.txt  

 

You can use old sed syntax along with bash for loop:

C代碼  
  1. #!/bin/bash  
  2. OLD="xyz"  
  3. NEW="abc"  
  4. DPATH="/home/you/foo/*.txt"  
  5. BPATH="/home/you/bakup/foo"  
  6. TFILE="/tmp/out.tmp.$$"  
  7. [ ! -d $BPATH ] && mkdir -p $BPATH || :  
  8. for f in $DPATH  
  9. do  
  10.   if [ -f $f -a -r $f ]; then  
  11.     /bin/cp -f $f $BPATH  
  12.    sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"  
  13.   else  
  14.    echo "Error: Cannot read $f"  
  15.   fi  
  16. done  
  17. /bin/rm $TFILE  

 

A Note About Bash Escape Character

A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored). This is useful when you would like to deal with UNIX paths. In this example, the sed command is used to replace UNIX path "/nfs/apache/logs/rawlogs/access.log" with "__DOMAIN_LOG_FILE__":

C代碼  
  1. #!/bin/bash  
  2. ## Our path  
  3. _r1="/nfs/apache/logs/rawlogs/access.log"  
  4.    
  5. ## Escape path for sed using bash find and replace   
  6. _r1="${_r1//\//\\/}"  
  7.    
  8. # replace __DOMAIN_LOG_FILE__ in our sample.awstats.conf  
  9. sed -e "s/__DOMAIN_LOG_FILE__/${_r1}/" /nfs/conf/awstats/sample.awstats.conf  > /nfs/apache/logs/awstats/awstats.conf  
  10.    
  11. # call awstats  
  12. /usr/bin/awstats -c /nfs/apache/logs/awstats/awstats.conf  

 

The $_r1 is escaped using bash find and replace parameter substitution syntax to replace each occurrence of/ with \/.

 

perl -pie Syntax For Find and Replace

The syntax is as follows:

C代碼  
  1. perl -pie 's/old-word/new-word/g' input.file > new.output.file  

 

來源:http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/

 

linux下查找某目錄下所有文件包含某字符串的命令

C代碼  
  1. #從文件內容查找匹配指定字符串的行:  
  2. $ grep "被查找的字符串" 文件名  
  3.   
  4. #從文件內容查找與正則表達式匹配的行:  
  5. $ grep –e “正則表達式” 文件名  
  6.   
  7. #查找時不區分大小寫:  
  8. $ grep –i "被查找的字符串" 文件名  
  9.   
  10. #查找匹配的行數:  
  11. $ grep -c "被查找的字符串" 文件名  
  12.   
  13. #從文件內容查找不匹配指定字符串的行:  
  14. $ grep –v "被查找的字符串" 文件名  
  15.   
  16. #從根目錄開始查找所有擴展名為.txt的文本文件,並找出包含"linux"的行  
  17. find . -type f -name "*.txt" | xargs grep "linux"   

 

來源:python腳本自動遷移


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM