linux下基于指定目录及子目录下所有文件中指定字符串进行替换

  1. 使用sed命令进行替换:

    sed -i 's/old_string/new_string/g' /path/to/directory/*
    
  2. 使用find命令结合sed进行替换:

    find /path/to/directory -type f -exec sed -i 's/old_string/new_string/g' {} +
    
  3. 使用grep命令找到包含指定字符串的文件,再使用sed进行替换:

    grep -rl 'old_string' /path/to/directory | xargs sed -i 's/old_string/new_string/g'
    
  4. 使用awk命令进行替换:

    awk '{gsub(/old_string/, "new_string"); print > FILENAME}' /path/to/directory/*
    
  5. 使用perl命令进行替换:

    perl -pi -e 's/old_string/new_string/g' /path/to/directory/*
    
  6. 使用Python脚本进行替换:

    import os
    
    def replace_string(path, old_string, new_string):
        for root, dirs, files in os.walk(path):
            for file in files:
                file_path = os.path.join(root, file)
                with open(file_path, 'r') as f:
                    content = f.read()
                content = content.replace(old_string, new_string)
                with open(file_path, 'w') as f:
                    f.write(content)
    
    replace_string('/path/to/directory', 'old_string', 'new_string')
    

最近更新

  1. TCP协议是安全的吗?

    2024-01-01 11:58:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-01 11:58:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-01 11:58:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-01 11:58:02       20 阅读

热门阅读

  1. leetcode中shell题解

    2024-01-01 11:58:02       37 阅读
  2. Spring Boot Actuator 功能介绍

    2024-01-01 11:58:02       32 阅读
  3. Redis 和 memcache 有什么区别?

    2024-01-01 11:58:02       40 阅读
  4. IDEA中查找实现类快捷键

    2024-01-01 11:58:02       42 阅读
  5. web自动化(3)——项目实战之流程用例编写

    2024-01-01 11:58:02       36 阅读
  6. 多浏览器密码修改,账户Token全部失效解决方案

    2024-01-01 11:58:02       51 阅读
  7. spingboot整合Swagger教程

    2024-01-01 11:58:02       40 阅读