linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
2015-10-22 15:50
886 查看
From: http://justcoding.iteye.com/blog/1948005
1. sed
C代码grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'例如:对象:文件夹C代码
grep -rl 'windows' ./path | xargs sed -i 's/windows/linux/g'
2. find
对象:文件C代码find -name 'test' | xargs perl -pi -e 's|windows|linux|g'这里使用了perl语言,使用-e加上一段代码,从而批量地将当前目录及所有子目录下的file.log文件中的string1替换成了string2; string支持正则表达式
3. awk
C代码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代码sed -i 's/old-word/new-word/g' *.txtGNU 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代码
sed 's/old/new/g' input.txt > output.txtYou can use old sed syntax along with bash for loop:C代码
#!/bin/bashOLD="xyz"NEW="abc"DPATH="/home/you/foo/*.txt"BPATH="/home/you/bakup/foo"TFILE="/tmp/out.tmp.$$"[ ! -d $BPATH ] && mkdir -p $BPATH || :for f in $DPATHdoif [ -f $f -a -r $f ]; then/bin/cp -f $f $BPATHsed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"elseecho "Error: Cannot read $f"fidone/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代码
#!/bin/bash## Our path_r1="/nfs/apache/logs/rawlogs/access.log"## Escape path for sed using bash find and replace_r1="${_r1//\//\\/}"# replace __DOMAIN_LOG_FILE__ in our sample.awstats.confsed -e "s/__DOMAIN_LOG_FILE__/${_r1}/" /nfs/conf/awstats/sample.awstats.conf > /nfs/apache/logs/awstats/awstats.conf# call awstats/usr/bin/awstats -c /nfs/apache/logs/awstats/awstats.confThe $_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代码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代码#从文件内容查找匹配指定字符串的行:$ grep "被查找的字符串" 文件名#从文件内容查找与正则表达式匹配的行:$ grep –e “正则表达式” 文件名#查找时不区分大小写:$ grep –i "被查找的字符串" 文件名#查找匹配的行数:$ grep -c "被查找的字符串" 文件名#从文件内容查找不匹配指定字符串的行:$ grep –v "被查找的字符串" 文件名#从根目录开始查找所有扩展名为.txt的文本文件,并找出包含"linux"的行find . -type f -name "*.txt" | xargs grep "linux"
相关文章推荐
- Linux 多线程调试(内存占用、死循环、CPU占用率高……)
- linux中的多窗口功能
- 每天一个linux命令(50):crontab命令
- LINUX下gdb无法debug,提示ImportError: No module named 'libstdcxx'
- 阿里云CentOS 7.1使用yum安装MySql5.6.24
- 树莓派准备工作--在无屏幕无ip情况下连接树莓派 linux
- 配置Centos 6.5的yum源
- linux安装flash player
- 嵌入式 Linux上的free命令详解
- diff命令详解
- Linux rpm 超级详细介绍及使用指导
- linux系统下的各种串口调试工具
- linux 中cut的用法
- 【LINUX】——linux如何使用Python创建一个web服务
- Linux rpm 命令参数使用详解[介绍和应用]
- Linux du,df 查看文件及文件夹大小
- Linux配置静态IP
- linux ln 建立软连接
- Linux学习17_每天一个linux命令(5):rm 命令
- Linux安装卸载Mysql数据库