g 全部替换 多文件替换 sed -i 's/old-text/new-text/g' * 或者单个命令利用xargs知行多次 grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @ -i@ 定义占位符为@ 参考: cyberciti.biz/faq/how-t How to replace a string in mul
Replaces all ‘OLDSTRING’ to ‘NEWSTRING’ in all files 1 $ grep -rl OLDSTRING * | sort | uniq | xargs sed -i -e ‘s/OLDSTRING/NEWSTRING/’ or using find: 1 find */public_html/ -iname '*.php' -exec sed -i -e 's/OLDSTRING/NEWSTRING/' {} \; ...
之前看到的sed命令会将每一行中第一处符合模式的内容替换掉。但是如果要替换所有内容,我们需要在命令尾部加上参数g,其方法如下:$ sed 's/pattern/replace_string/g' file 后缀/g意味着sed会替换每一处匹配。但是有时候我们只需要从第n处匹配开始替换。对此,可以使用/Ng选项。 $echothisthisthisthis|sed's/this...
Now, let’s use sed to replace a string on the nth line of multiple text files: $ find . -type f -name '*.txt' -exec sed -i '2s/yahoo/gmail/' {} + Let’s examine the above command: find . -type f -name ‘*.txt’ –searches for all .txt files in the present working ...
sed 使用s/regexp/replacement/命令来替换匹配特定模式的内容,man sed 的说明如下: Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which ...
sed -i -- 's/foo/bar/g' "$file" fi done ) 1. 2. 3. 4. 5. 6. 7. The files are selected when they are actual files (-f) and they are writable (-w). 4. Multiple replace operations: replace with different strings You can combinesedcommands: ...
# Linux shell multifile content replace withsed# 声明: # 本源代码主要是利用两份(中、英文)具有相同键值对的json数据,对html内的中文进行 # 自动化文本替换的代码。 # #2015-11-20晴 深圳 南山平山村 曾剑锋 # 得到中文部分sed-n"/\"/p"SimpChinese.txt |grep-Po -e"\"\s?:\s?.*"|grep-Po ...
sed-f script_file input_file 下面通过一些实际操作说明一下sed(未加说明即是指sed(GNUsed)4.2.2,下同)常用参数的含义和用法 首先获得实验文本 cv@cv: ~/myfiles$touchtest.txt cv@cv:~/myfiles$mansed|head-n30|tail-n28>test.txt cv@cv:~/myfiles$cattest.txt ...
#在第二行前插入一行sed'2 i insert_context'filename#在第二行之后插入一行sed'2 a insert_context'filename#在匹配的行之前插入一行sed'/pattern/i\new_word'filename 打印 #只打印出第一行 ,不加n的话会默认输出每一行sed -n'1p'filename#只打印出被修改的一行sed -n's/the/THE/p'filename ...
sed My favorite use forsedis to replace strings in files. For example: $catquotes.txt|sed's/universe/Universe/g' This will replaceuniversewithUniverseand send the result tostdout. Thegflag means "replace all occurrences of the string in each line." ...