批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容) 字符串全局替换

实践:

grep -rl '10.222.1.55' ./ | xargs sed -i 's/10.222.1.55/10.222.1.57/g'

 

 vim的全局替换_D_Double's Journey-CSDN博客_vi全局替换 https://blog.csdn.net/shuangde800/article/details/10554513

本文是在学习《使用vi编辑器, Lamb & Robbins编著》时在所记的笔记。

本文内容:
基本全局替换
确认替换
上下文相关替换

本文约定:
$ + 命令 表示在普通的bash的命令行下
: + 命令 表示在vim的ex(命令)模式, 按"Ctrl" + ":" 进入

------------------------------------------------------------------------------------------------


替换命令的基本语法

在全局替换中,ex编辑器使用指定的字符模式对文件中的每行进行检查。在所有行中发现模式的位置,ex使用新字符串来替换模式。

全局替换真正使用的是两个ex命令是 :g(global全局)和:s(switch替换)。

替换命令的语法如下:
:s / old / new /
这将把当前中模式old的第一次出现修改为new。 /(斜杆)是命令不同部分之间的分隔符(当斜杆为该行的最后一个字符时,可不写)

下面这种形式的替换命令:
:s / old / new / g
把当前行old的每次出现改为new,而不只是该行的第一个old。:s命令允许替换串后面带有选项,上面语法中的g代表全局(g选项影响一行中的每个模式,不要把它与影响文件中所有行的:g命令混淆)

通过在:s命令前加上地址前缀,可以把它的范围扩展到多行。例如下面命令把50行到100行的old的每次出现都改为new:
:50, 100 s / old / new / g

下面的命令把整个文件中的old的每次出现都改为new:
:1, $s / old / new / g
我们知道%等价于1,$,所以上行命令也可以这样写:
:% s / old / new / g

 

确认替换

使用搜索替换命令有时候会出错,而得到不想要的结果。所以小心并确认文件中需要修改的内容是一个明智的做法

在替换命令尾部加上c (confirm用于确认),在替换每个old前都会提示并确认:
:1, 30 s /old/new/ gc
将会出现提示replace with hehe (y/n/a/q/l/^E/^Y)?
y替换,n不替换,a替换所有,q放弃,l替换第一个并进入插入模式,^E和^Y是提示你用Ctrl+e或Ctrl+y来滚动屏幕的。

 

上下文相关替换

除了直接使用一个单词(或短语)替换另一个,还有稍微复杂的全局替换语法。这些语法可以对一个模式进行搜索,一旦找到含有模式的行,就可以使用不同与模式的串进行替换,我们把这种替换叫做上下文相关替换。

语法格式如下:
:g /pattern/s/old/new/g 将会把包含pattern的行中,把所有old替换为new
第一个g表示是在文件的所有行上执行的命令,模式pattern识别要发生替换的行。在那些包含模式pattern的行上,ex将把old替换(s)为new。最后的g表示在该行上进行全部替换。

如果用来进行搜索的模式与想要修改的模式相同,那么就不必重复它:
:g/string/s//new/g 等价于 :g/string/s/string/new/g
还要注意:
:g/string/s//new/g 也等价于 :% s/string/new/g
可以用第二种方式少输一些字符
————————————————
版权声明:本文为CSDN博主「shuangde800」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shuangde800/article/details/10554513

 

 

文件查找  find / | grep "mysql\.h"

 


Administrator@PC-20160727BOAG MINGW64 /d/xcx0627/pages (master)
$ find . -type f -name "*.js" | xargs sed -i 's/var i in v/let i in v/g'

 

linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容) - Just Code - ITeye博客 https://www.iteye.com/blog/justcoding-1948005

 

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"   

 

 

 

 

var v = res.data.data
for (let i in v) {
partnerSiteClicked.push(v[i].site_id)
}
 
 
 

 

posted @ 2018-07-30 20:16  papering  阅读(1182)  评论(0编辑  收藏  举报