linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
来源:互联网 发布:matlab cell数组 编辑:程序博客网 时间:2024/05/18 00:09
From: http://justcoding.iteye.com/blog/1948005
1. sed
- grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'
例如:
对象:文件夹
- grep -rl 'windows' ./path | xargs sed -i 's/windows/linux/g'
2. find
对象:文件
- find -name 'test' | xargs perl -pi -e 's|windows|linux|g'
3. awk
- grep -i "windows" -r ./path | awk -F : '{print $1}' | sort | uniq | xargs sed -i 's/windows/linux/g'
sed replace word / string syntax
The syntax is as follows:
- 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:
- sed 's/old/new/g' input.txt > output.txt
You can use old sed syntax along with bash for loop:
- #!/bin/bash
- OLD="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 $DPATH
- do
- if [ -f $f -a -r $f ]; then
- /bin/cp -f $f $BPATH
- sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
- else
- echo "Error: Cannot read $f"
- fi
- done
- /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__":
- #!/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.conf
- sed -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.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:
- 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下查找某目录下所有文件包含某字符串的命令
- #从文件内容查找匹配指定字符串的行:
- $ grep "被查找的字符串" 文件名
- #从文件内容查找与正则表达式匹配的行:
- $ grep –e “正则表达式” 文件名
- #查找时不区分大小写:
- $ grep –i "被查找的字符串" 文件名
- #查找匹配的行数:
- $ grep -c "被查找的字符串" 文件名
- #从文件内容查找不匹配指定字符串的行:
- $ grep –v "被查找的字符串" 文件名
- #从根目录开始查找所有扩展名为.txt的文本文件,并找出包含"linux"的行
- find . -type f -name "*.txt" | xargs grep "linux"
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- linux下批量修改目录下所有文件夹、文件名称及文件内容
- linux-sed命令: 批量替换某文件夹下所有文件里的某段内容
- linux 查找某目录下包含关键字内容的文件
- linux 批量查找匹配文件 (批量修改内容)
- linux下如何批量查找或替换文件中的内容?
- linux下用命令批量修改替换文件内容
- Linux系统下批量替换文件内容
- Linux下批量替换文件内容方法
- Linux 下批量替换文件内容
- linux下批量替换文件内容
- linux下批量替换文件内容
- linux下批量替换文件内容
- linux下批量替换文件内容
- linux下批量替换文件内容
- linux下批量替换文件内容
- Litepal 混淆注意
- jQuery的$.post请求传值——返回值为json格式
- json api
- zlib库剖析(1):实现概览
- 程序员书单_UML篇
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- 九度考研真题 2003-1华中1185:特殊排序 排序
- myeclipse构建用户库
- UIPickerView的简单使用
- Android 百度地图可以定位,但无法显示地图 的解决办法
- Bezier曲线的几何连续性
- Objective-C 程序设计 第六章
- 数据上报-上报设计
- Linux下能够创建的最大线程数