for i in *;do sed -ie 's/_test2/_test3/g' $i; sed -ie 's/_type2/_type3/g' $i; done
|
|
1. Replacing all occurrences of one string with another in all files in the current directory:
These are for cases where you know that the directory contains only regular files and that you want to process all non-hidden files. If that is not the case, use the approaches in 2.
All sed solutions in this answer assume GNU sed. If using FreeBSD or OS/X, replace -i with -i ''. Also note that the use of the -i switch with any version of sed has certain filesystem security implications and is inadvisable in any script which you plan to distribute in any way.
Non recursive, files in this directory only:
sed -i -- 's/foo/bar/g' *
perl -i -pe 's/foo/bar/g' ./*
(the perl one will fail for file names ending in | or space)).
Recursive, regular files (including hidden ones) in this and all subdirectories
find . -type f -exec sed -i 's/foo/bar/g' {} +
If you are using zsh:
sed -i -- 's/foo/bar/g' **/*(D.)
(may fail if the list is too big, see zargs to work around).
Bash can't check directly for regular files, a loop is needed (braces avoid setting the options globally):
( shopt -s globstar dotglob;
for file in **; do
if [[ -f $file ]] && [[ -w $file ]]; then
sed -i -- 's/foo/bar/g' "$file"
fi
done
)
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 combine sed commands:
sed -i 's/foo/bar/g; s/baz/zab/g; s/Alice/Joan/g' file
Be aware that order matters (sed 's/foo/bar/g; s/bar/baz/g' will substitute foo with baz).
or Perl commands
perl -i -pe 's/foo/bar/g; s/baz/zab/g; s/Alice/Joan/g' file
If you have a large number of patterns, it is easier to save your patterns and their replacements in a sed script file:
#! /usr/bin/sed -f
s/foo/bar/g
s/baz/zab/g
Or, if you have too many pattern pairs for the above to be feasible, you can read pattern pairs from a file (two space separated patterns, $pattern and $replacement, per line):
while read -r pattern replacement; do
sed -i "s/$pattern/$replacement/" file
done < patterns.txt
That will be quite slow for long lists of patterns and large data files so you might want to read the patterns and create a sed script from them instead. The following assumes a <space> delimiter separates a list of MATCH<space>REPLACE pairs occurring one-per-line in the file patterns.txt :
sed 's| *\([^ ]*\) *\([^ ]*\).*|s/\1/\2/g|' <patterns.txt |
sed -f- ./editfile >outfile
The above format is largely arbitrary and, for example, doesn't allow for a <space> in either ofMATCH or REPLACE. The method is very general though: basically, if you can create an output stream which looks like a sed script, then you can source that stream as a sed script by specifying sed's script file as -stdin.
You can combine and concatenate multiple scripts in similar fashion:
SOME_PIPELINE |
sed -e'#some expression script' \
-f./script_file -f- \
-e'#more inline expressions' \
./actual_edit_file >./outfile
A POSIX sed will concatenate all scripts into one in the order they appear on the command-line. None of these need end in a \newline.
grep can work the same way:
sed -e'#generate a pattern list' <in |
grep -f- ./grepped_file
When working with fixed-strings as patterns, it is good practice to escape regular expressionmetacharacters. You can do this rather easily:
sed 's/[]$&^*\./[]/\\&/g
s| *\([^ ]*\) *\([^ ]*\).*|s/\1/\2/g|
' <patterns.txt |
sed -f- ./editfile >outfile
5. Multiple replace operations: replace multiple patterns with the same string
Replace any of foo, bar or baz with foobar
sed -Ei 's/foo|bar|baz/foobar/g' file
or
perl -i -pe 's/foo|bar|baz/foobar/g' file
|
- linux替换目录下所有文件中的某字符串
linux替换目录下所有文件中的某字符串 比如,要将目录/modules下面所有文件中的zhangsan都修改成lisi,这样做: sed -i "s/zhangsan/lisi/g&quo ...
- linux获得目录下文件个数
获得当前目录下文件个数赋值给变量panonum: panonum=$(ls -l |grep "^-" | wc -l) 获取指定目录下文件个数赋值给指定变量: panonum=$ ...
- Linux统计目录下文件个数及代码行数
1. 统计当前目录下,php文件数量 find ./ -name "*.php" | wc -l 2. 统计当前目录下所有php文件代码行数 find ./ -name " ...
- linux获取目录下文件
查看当前目录下的文件: find . -type f 查看当前目录下的文件夹: find . -type d 如果文件file1不为空: if [ -s file1 ];then echo ...
- linux 查询目录下包含关键字的所有文件
linux查找目录下的所有文件中是否含有某个字符串 查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有 ...
- Linux查找和替换目录下所有文件中字符串(转载)
转自:http://rubyer.me/blog/1613/ 单个文件中查找替换很简单,就不说了.文件夹下所有文件中字符串的查找替换就要记忆了,最近部署几十台linux服务器,记录下总结. 查找文件夹 ...
- linux 目录下文件批量植入和删除,按日期打包
linux目录下文件批量植入 [root@greymouster http2]# find /usr/local/http2/htdocs/ -type f|xargs sed -i " ...
- Linux中/proc目录下文件详解
转载于:http://blog.chinaunix.net/uid-10449864-id-2956854.html Linux中/proc目录下文件详解(一)/proc文件系统下的多种文件提供的系统 ...
- Linux常用基础命令整理:关机命令、查看目录下文件命令等
Linux常用基础命令整理:关机命令.查看目录下文件命令等 整理了一些Linux常用基础命令,欢迎指正. 首先记住四个热键,学会这四个键,收益一辈子. Tab按键---命令补齐功能Ctrl+c按键-- ...
随机推荐
- Linux驱动platform
platform device<==> platform bus <==> platform driver 转自:platform设备驱动全透析 宋宝华 http://blog ...
- sql exist 优化查询时间
1.非exist,查询需要20多秒 2.使用exist后 3.表连接也能优化
- [转]Unity Shader 学习总结
1.先来一段单张纹理贴图的shader示例代码: // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClip ...
- IP地址、子网掩码、网关的关系
网络管理中的IP地址.子网掩码和网关是每个网管必须要掌握的基础知识,只有掌握它,才能够真正理解TCP/IP协议的设置.以下我们就来深入浅出地讲解什么是子网掩码. IP地址的结构 要想理解什么是子网掩码 ...
- Eclipse搭建C++开发环境
http://jingyan.baidu.com/article/456c463b67b4310a59314468.html
- Selenium+C#自动化脚本开发学习
1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver driver = new FirefoxDriver(); //打开指定的URL地址 driver.Navigate ...
- SET IDENTITY_INSERT ON/OFF 权限
今天突然遇到了,找不到对象“XXXX”,因为它不存在或者没有您所需的权限,于是检查程序,突然发现程序中有一段代码是: SET IDENTITY_INSERT eticket ON //执行业务 ... ...
- ACboy needs your help(简单DP)
HDU 1712 Problem Description ACboy has N courses this term, and he plans to spend at most M days on ...
- 九度OJ 1202:排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:19711 解决:6508 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100). ...
- word2vec_basic.py
ssh://sci@192.168.67.128:22/usr/bin/python3 -u /home/win_pymine_clean/feature_wifi/word2vec_basic.py ...