linux 批量替换文件内容及查找某目录下所有包含某字符串的文件
转载请注明来源:https://www.cnblogs.com/hookjc/
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'
这里使用了shell命令,先查找出文件,再用awk分割(以:切分),再行替换!
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"
来源:python脚本自动迁移
linux 批量替换文件内容及查找某目录下所有包含某字符串的文件的更多相关文章
- 运维笔记--Linux查找指定目录下某段时间的文件
查找指定目录下,60天之前的文件:find /mnt/xml_data -mtime +60 -name "*.xml" 找到并统计数量:find /mnt/xml_data -m ...
- Python3 批量替换文本内容
Python3 批量替换文本内容 示例: # coding:utf8 import os; def reset(): i = 0 path = r"H:\asDemo\workdemo\aw ...
- sql数据库批量替换dedecms内容关键字
之前写了一篇dedecms后台批量替换文章中的关键词,这边我们介绍一下用sql数据库批量替换dedecms内容关键字,当然要求你对数据库比较熟悉,修改前请自行做好备份. 1.更改文章中的内容 upda ...
- 批量替换存储过程内容脚本sp_SqlReplace
开始 在数据库开发过程中,如果某一个表字段名被重命名.那么在使用到该字段的存储过程,对应的表字段名也要修改. 当存在多个存储都有使用该表字段,需要逐个去修改替换,是一件比较繁琐的事情,我们需要一个能实 ...
- 帝国cms 批量替换 字段内容包含的 指定的 关键字 SQL命令
帝国cms 批量替换 字段内容包含的 指定的 关键字update phome_ecms_news_data_1 set newstext=replace(newstext,'原来','现在');
- [bash]查找指定目录下符合格式的txt文件
需求: 查找指定目录下符合yyyy-MM-dd(-b)NNN.txt格式的文件,如“2020-03-22-b888.txt” 目标目录内容: [root@localhost bashs]# ll /r ...
- C++查找指定目录下所以指定类型的文件
/*************************************************************** 函数名称:FindFile 查找指定目录下指定文件 输入:fileNa ...
- python_自动查找指定目录下的文件或目录的方法
代码如下 import os def find_file(search_path, file_type="file", filename=None, file_startswith ...
- CustomEditor的文件要放在Assets/Editor目录下
using UnityEditor; using UnityEngine; [CustomEditor(typeof(test))] public class testEditor : Editor ...
随机推荐
- IM2603 Type-C扩展坞电源管理 IC
IM2603 概述 用于带有集成降压转换器的 Type-C 外围应用的电源管理 IC IM2603 是一款主要用于 Type-C 外围应用的电源管理 IC. 它集成了一个带有内置高侧 MOSFET 的 ...
- 带SD读卡的USB HUB方案芯片MA8621|用于带读卡的USB HUB拓展坞方案芯片MA8621
MA8621是一款带SD读卡器控制器的USB 2.0高速3端口集线器方案芯片,主要用在USB TYPEC拓展坞或者USB typec扩展底座上面. 1. MA8621功能概述 MA8621是USB 2 ...
- 论文翻译:2020_Acoustic Echo Cancellation Based on Recurrent Neural Network
论文地址:https://ieeexplore.ieee.org/abstract/document/9306224 基于RNN的回声消除 摘要 本文提出了一种基于深度学习的语音分离技术的回声消除方法 ...
- ActiveMQ基础教程(二):安装与配置(单机与集群)
因为本文会用到集群介绍,因此准备了三台虚拟机(当然读者也可以使用一个虚拟机,然后使用不同的端口来模拟实现伪集群): 192.168.209.133 test1 192.168.209.134 test ...
- Zabbix忘记用户名和密码的解决方法
问题描述: 最近刚搭建了zabbix服务器,然后新增加了一个用户,并且把默认的Admin禁用了.然后这个监控页面一直没关,保持了10多天,今天不小心把浏览器关闭了,重新打开后,突然忘记了用户名,这下麻 ...
- js获取设备内网ip
可以直接使用,不需要导入其他配置 看代码 1 <script> 2 //获取内网ip 3 var RTCPeerConnection = window.RTCPeerConnection ...
- 用jquery实现省市联动
<!-- 需求: [1] 动态生成省份选择框. [2] 当选择了省份的某一项时, 动态改变 城市选择中的列表项. --> <!DOCTYPE html> <html la ...
- Struts-S2-045漏洞利用
最近也是在看Struts2的漏洞,这里与大家共同探讨一下,本次我复现的是s2-045这个编号的漏洞 漏洞介绍 Apache Struts 2被曝存在远程命令执行漏洞,漏洞编号S2-045,CVE编号C ...
- springboot打包第三方jar包是失败
在项目开发时有时我们需要引入一些在maven仓库中不存在的包 一.配置maven环境变量 在path环境变量中添加 %maven_home%\bin (window10环境下) cmd界面输入 mvn ...
- 《手把手教你》系列技巧篇(五十八)-java+ selenium自动化测试-分页测试(详细教程)
1.简介 前几天,有人私信里留言问宏哥,分页怎么自动化测试了,完了给他说了说思路,不知道最后搞定没有,索性宏哥就写一篇文章来讲解和介绍如何处理分页. 2.测试场景 对分页来说,我们最感兴趣的和测试的无 ...