zoukankan      html  css  js  c++  java
  • linux --批量修改文件内容

    由于目前测试的BIOS有一个option 发生了改变,因此我们需要在之前写好的脚本上进行修改,将旧的option 改为新的选项,因此在此处用到了批量修改文件中的内容;

    1. perl 命令替换:

    perl -i -e "s/old/new/g" the path of the file

    下面,就将test1 text2,中的cat 都换成了dog,汪汪~~

    [root@11 tmp]# touch test1.txt
    [root@11 tmp]# vim test1.txt
    [root@11 tmp]# cat test1.txt
    cat cat cat
    i like linux
    i like windows too
    i like watching TV
    I like chenqingling
    
    [root@11 tmp]# cp test1.txt text2.txt
    [root@11 tmp]# cp test1.txt text3.txt
    [root@11 tmp]# cp test1.txt text4.txt
    [root@11 tmp]# perl -p -i -e "s/cat/dog/g" test1.txt text2.txt
    [root@11 tmp]# cat test1.txt
    dog dog dog
    i like linux
    i like windows too
    i like watching TV
    I like chenqingling
    
    [root@11 tmp]# cat text2.txt
    dog dog dog
    i like linux
    i like windows too
    i like watching TV
    I like chenqingling
    
    [root@11 tmp]# cat text3.txt
    cat cat cat
    i like linux
    i like windows too
    i like watching TV
    I like chenqingling

    2.运用sed 命令批量修改文件内容:

    sed -i "s/old/new/g" the path of the files

    修改text2,text3,中的linux ,修改为Unix

    [root@11 tmp]# sed -i "s/linux/Unix/g" text2.txt text3.txt
    [root@11 tmp]# cat text2.txt
    dog dog dog
    i like Unix
    i like windows too
    i like watching TV
    I like chenqingling
    
    [root@11 tmp]# cat text3.txt
    cat cat cat
    i like Unix
    i like windows too
    i like watching TV
    I like chenqingling
    
    [root@11 tmp]# cat test1.txt
    dog dog dog
    i like linux
    i like windows too
    i like watching TV
    I like chenqingling

     3.补充点:将old 全部换成 new 

    sed -i “s/old/new/g” `grep old -rl /path`    #当前路径表示:./

    [root@110 lu]# sed -i "s/TV/xiaozhan/g" `grep TV -rl ./` #注意这里的`` 不是单引号!!
    [root@11 lu]# cat text2.txt
    dog dog dog
    i like Unix
    i like windows too
    i like watching xiaozhan
    I like chenqingling
    
    [root@11 lu]# cat text3.txt
    cat cat cat
    i like Unix
    i like windows too
    i like watching xiaozhan
    I like chenqingling
    
    [root@11 lu]# cat text4.txt
    cat cat cat
    i like linux
    i like windows too
    i like watching xiaozhan
    I like chenqingling
  • 相关阅读:
    Spring Cloud
    Java/Android 网络请求框架/库
    Spring Roo 想知道源码,怎么实现自动生成枯燥的有规律的文件
    win10操作系统系统,小米路由器,小米3 的问题
    IntelliJ IDEA
    Spring Boot
    echart 插件实现全国地图
    安装AndroidJDK的坑
    小程序进阶之路
    关于echart柱形图的使用问题
  • 原文地址:https://www.cnblogs.com/clairedandan/p/11458947.html
Copyright © 2011-2022 走看看