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
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
    记一次odoo创建新的模块时,但是在odoo web界面找不到应用的案例
    python实现格式化输出9*9乘法表
    format和urlencode的使用对比
    python字典小知识
    01
    深浅拷贝再回顾
    DRF的路由生成类的使用
  • 原文地址:https://www.cnblogs.com/clairedandan/p/11458947.html
Copyright © 2011-2022 走看看