• [linux] 替换字符串


    Linux下批量替换多个文件中的字符串的简单方法。用sed命令可以批量替换多个文件中的字符串。

    命令如下:sed -i “s/原字符串/新字符串/g” `grep 原字符串 -rl 所在目录`

    例如:我要把 charset=gb2312 替换为 charset=UTF-8,执行命令:sed -i “s/charset=gb2312/charset=UTF-8/g” ‘grep charset=gb2312 -rl /www’ 即可。

    如果把文件按竖线"|"或分号";"分割替换成hive的"1"分割

    sed -i 's/|/x1/g;
    s/;/x1/g
    ' a.txt

    把一个[文件目录][类别目录][日期目录]*.tar.gz改成gzip格式,并替换分隔符,换成hive的"1"分割

    for dir in `ls .`
     do
       if [ -d $dir ]
       then
         echo $dir
         cd $dir
    
     for dir in `ls .`
     do
       if [ -d $dir ]
       then
         echo $dir
         cd $dir
         find . -mindepth 1 -name "*.tar.gz" -type f -exec tar -zxvf {} ; -exec rm -rf {} ; 
         find . -mindepth 1 -name "*.*" -type f -exec sed -i 's/|/x1/g;s/;/x1/g' {} ; -exec gzip -r {} ; 
         cd ..
       fi
    done
    
         cd ..
       fi
    done
  • 相关阅读:
    POJ3678 KATU PUZZLE
    poj3321(codevs1228)苹果树
    codevs 1955 光纤通信 USACO
    codevs 1027 姓名与ID
    codevs 1051 接龙游戏
    洛谷 P1717 钓鱼
    codevs 1062 路由选择
    洛谷 P1083 借教室
    codevs 2596 售货员的难题
    Vijos 1053 easy sssp
  • 原文地址:https://www.cnblogs.com/linn/p/5711741.html
Copyright © 2020-2023  润新知