转载请注明来源: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. Java初学者作业——编写程序计算实发工资(实践1)

    返回本章节 返回作业目录 需求说明: 腾讯为Java工程师提供了基本工资(8000元).物价津贴及房租津贴.其中物价津贴为基本工资的40%,房屋津贴为基本工资的25%.要求编写程序计算实发工资. 实现 ...

  2. JSP请求响应流程入门介绍

    一个完整的jsp请求响应流程可以简单的使用下图表示: 过滤器:直观的了解,就是对请求做一个过滤作用,比如身份验证,验证不通过的不让他继续往下走 Servlet:请求处理中心,这个也是我们写业务逻辑的地 ...

  3. CSS基础 精灵图的使用

    使用步骤1.创建盒子     <div class="one"></div>   2.使用PxCook量取图标大小,将图标的宽高设置成为盒子的宽高      ...

  4. .NET 云原生架构师训练营(组合模式)--学习笔记

    目录 引入 组合模式 源码 引入 在上一篇执行 _connectionDelegate 之后,HttpConnectionMiddleware 处理请求 return connection.Proce ...

  5. 单例模式(python)

    python 的单例模式需要重写__new__()和 __init__() 需要注意,标识符__和_区别 参考资料: https://www.cnblogs.com/huchong/p/8244279 ...

  6. yum方式安装nginx

    1.添加CentOS 7 Nginx yum资源库 [root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPM ...

  7. Servlet初级学习加入数据库操作(一)

    需要的源代码地址: https://url56.ctfile.com/f/34653256-527822631-2e255a(访问密码:7567) 将页面中的数据逐步替换为数据库管理 准备一个连接数据 ...

  8. 【爬虫】将 Scrapy 部署到 k8s

    一. 概述 因为学习了 docker 和 k8s ,不管什么项目都想使用容器化部署,一个最主要的原因是,使用容器化部署是真的方便.上一篇文章 [爬虫]从零开始使用 Scrapy 介绍了如何使用 scr ...

  9. 机器学习&恶意代码动态检测

    目录 写在前面 1 基于API调用的统计特征 2 API序列特征 3 API调用图 4 基于行为的特征 references: 写在前面 对恶意程序动态检测方法做了概述, 关于方法1和2可以参考阿里云 ...

  10. Web开发之Cookie and Session

    会话 什么是会话? 简单说:用户开一个浏览器,点击多个超链接,访问服务器的多个web资源,然后关闭浏览器,整个过程就称之为一个会话. 会话过程要解决什么问题 每个用户在使用浏览器与服务器进行会话的过程 ...