转载请注明来源:https://www.cnblogs.com/hookjc/

1. sed

C代码  
  1. grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

例如:

对象:文件夹

C代码  
  1. grep -rl 'windows' ./path | xargs sed -i 's/windows/linux/g'

2. find

对象:文件

C代码  
  1. find -name 'test' | xargs perl -pi -e 's|windows|linux|g'
 这里使用了perl语言,使用-e加上一段代码,从而批量地将当前目录及所有子目录下的file.log文件中的string1替换成了string2; string支持正则表达式
 

3. awk

C代码  
  1. 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代码  
  1. 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:

C代码  
  1. sed 's/old/new/g' input.txt > output.txt

 

You can use old sed syntax along with bash for loop:

C代码  
  1. #!/bin/bash
  2. OLD="xyz"
  3. NEW="abc"
  4. DPATH="/home/you/foo/*.txt"
  5. BPATH="/home/you/bakup/foo"
  6. TFILE="/tmp/out.tmp.$$"
  7. [ ! -d $BPATH ] && mkdir -p $BPATH || :
  8. for f in $DPATH
  9. do
  10. if [ -f $f -a -r $f ]; then
  11. /bin/cp -f $f $BPATH
  12. sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
  13. else
  14. echo "Error: Cannot read $f"
  15. fi
  16. done
  17. /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代码  
  1. #!/bin/bash
  2. ## Our path
  3. _r1="/nfs/apache/logs/rawlogs/access.log"
  4. ## Escape path for sed using bash find and replace
  5. _r1="${_r1//\//\\/}"
  6. # replace __DOMAIN_LOG_FILE__ in our sample.awstats.conf
  7. sed -e "s/__DOMAIN_LOG_FILE__/${_r1}/" /nfs/conf/awstats/sample.awstats.conf  > /nfs/apache/logs/awstats/awstats.conf
  8. # call awstats
  9. /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:

C代码  
  1. 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代码  
  1. #从文件内容查找匹配指定字符串的行:
  2. $ grep "被查找的字符串" 文件名
  3. #从文件内容查找与正则表达式匹配的行:
  4. $ grep –e “正则表达式” 文件名
  5. #查找时不区分大小写:
  6. $ grep –i "被查找的字符串" 文件名
  7. #查找匹配的行数:
  8. $ grep -c "被查找的字符串" 文件名
  9. #从文件内容查找不匹配指定字符串的行:
  10. $ grep –v "被查找的字符串" 文件名
  11. #从根目录开始查找所有扩展名为.txt的文本文件,并找出包含"linux"的行
  12. find . -type f -name "*.txt" | xargs grep "linux"

来源:python脚本自动迁移

linux 批量替换文件内容及查找某目录下所有包含某字符串的文件的更多相关文章

  1. 运维笔记--Linux查找指定目录下某段时间的文件

    查找指定目录下,60天之前的文件:find /mnt/xml_data -mtime +60 -name "*.xml" 找到并统计数量:find /mnt/xml_data -m ...

  2. Python3 批量替换文本内容

    Python3 批量替换文本内容 示例: # coding:utf8 import os; def reset(): i = 0 path = r"H:\asDemo\workdemo\aw ...

  3. sql数据库批量替换dedecms内容关键字

    之前写了一篇dedecms后台批量替换文章中的关键词,这边我们介绍一下用sql数据库批量替换dedecms内容关键字,当然要求你对数据库比较熟悉,修改前请自行做好备份. 1.更改文章中的内容 upda ...

  4. 批量替换存储过程内容脚本sp_SqlReplace

    开始 在数据库开发过程中,如果某一个表字段名被重命名.那么在使用到该字段的存储过程,对应的表字段名也要修改. 当存在多个存储都有使用该表字段,需要逐个去修改替换,是一件比较繁琐的事情,我们需要一个能实 ...

  5. 帝国cms 批量替换 字段内容包含的 指定的 关键字 SQL命令

    帝国cms 批量替换 字段内容包含的 指定的 关键字update phome_ecms_news_data_1 set newstext=replace(newstext,'原来','现在');

  6. [bash]查找指定目录下符合格式的txt文件

    需求: 查找指定目录下符合yyyy-MM-dd(-b)NNN.txt格式的文件,如“2020-03-22-b888.txt” 目标目录内容: [root@localhost bashs]# ll /r ...

  7. C++查找指定目录下所以指定类型的文件

    /*************************************************************** 函数名称:FindFile 查找指定目录下指定文件 输入:fileNa ...

  8. python_自动查找指定目录下的文件或目录的方法

    代码如下 import os def find_file(search_path, file_type="file", filename=None, file_startswith ...

  9. CustomEditor的文件要放在Assets/Editor目录下

    using UnityEditor; using UnityEngine; [CustomEditor(typeof(test))] public class testEditor : Editor ...

随机推荐

  1. Vue.js高效前端开发 • 【Ant Design of Vue框架进阶】

    全部章节 >>>> 文章目录 一.栅格组件 1.栅格组件介绍 2.栅格组件使用 3.实践练习 二.输入组件 1.输入框组件使用 2.选择器组件使用 3.单选框组件使用 4.实践 ...

  2. Shell自动上传下载文件到SFTP服务器

    1.说明 本文提供一个Shell脚本, 可以自动连接到SFTP服务器, 然后上传或者下载指定的文件, 进而可以使用Linux的corntab命令, 定时执行脚本上传下载文件, 实现文件的同步或者备份功 ...

  3. [login] 调用失败 Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID , cloud function service error code -501000, error message Environment not found;

    按照微信开放文档,创建完云开发项目,运行,点击获取openid,报如下错: [login] 调用失败 Error: errCode: -404011 cloud function execution ...

  4. MySQL 中如何定位 DDL 被阻塞的问题

    经常碰到开发.测试童鞋会问,线下开发.测试环境,执行了一个DDL,发现很久都没有执行完,是不是被阻塞了?要怎么解决? 包括在群里,也经常会碰到类似问题:DDL 被阻塞了,如何找到阻塞它的 SQL ? ...

  5. Visual Studio 2022(VS2022)激活密钥

    Visual Studio 2022(VS2022) 激活码: 专业版 Pro: TD244-P4NB7-YQ6XK-Y8MMM-YWV2J 企业版 Enterprise: VHF9H-NXBBB-6 ...

  6. day1 三位数各个位上的数字和

    int main(){ int x = 0; scanf("%d", &x); if (x > 999 || x < 0) { printf("输入错 ...

  7. F2BPM的流程仿真

    仿真概述 F2BPM工作流仿真是一种通过建立工作流虚拟运行环境执行工作流仿真的方法.集中式仿真引擎解释工作流仿真模型,仿真活动的执行,处理仿真过程中的不确定性,从而完成工作流模型的仿真.同时,会实时显 ...

  8. 从头造轮子:python3 asyncio之 gather (3)

    前言 书接上文:,本文造第三个轮子,也是asyncio包里面非常常用的一个函数gather 一.知识准备 ● 相对于前两个函数,gather的使用频率更高,因为它支持多个协程任务"同时&qu ...

  9. JavaScript中数字常用方法

    数字常用方法 (1)返回0-1(不包含1)随机小数(random()) 格式:Math.random() var num=parseInt(Math.random()*11) //拿到0-10中的随机 ...

  10. Docker环境安装,基本命令集合

    一.docker安装 1).卸载旧的安装包 centos7默认安装的docker版本是1.13.1,卸载它,安装新的版本. root用户下,一次把这坨命令复制进去 yum remove docker ...