zoukankan      html  css  js  c++  java
  • vi编辑字符串替换及sed用法

    1、vi编辑替换文件中的字符串

    :s/nice/good/ 替换当前行第一个 nice为 good

    :s/nice/good/g 替换当前行所有 nice 为 good

    :n,$s/nice/good/ 替换第 n 行开始到最后一行中每一行的第一个 nice 为 good

    :n,$s/nice/good/g 替换第 n 行开始到最后一行中每一行所有 nice 为 good

    n 为数字,若 n 为 .,表示从当前行开始到最后一行

    :%s/nice/good/(等同于 :g/nice/s//good/) 替换每一行的第一个 nice 为 good

    :%s/nice/good/g(等同于 :g/nice/s//good/g) 替换每一行中所有 nice 为 good

    可以使用 # 作为分隔符,此时中间出现的 / 不会作为分隔符

    :s#nice/#good/# 替换当前行第一个 nice/ 为 good/

    :%s#/usr/bin#/bin#g

    把文件中所有路径/usr/bin换成/bin

    2、sed -i批量替换文件中的字符串

    格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径`
          sed -i "s/oldstring/newstring/g" `grep oldstring -rl yourdir`
        例如:替换/home/ock下所有文件中的nice为book
        sed -i "s/nice/book/g" `grep nice -rl /home`
        exp:sed -i "s/shabi/$/g" `grep shabi -rl ./`

        将文件1.txt内的文字“garden”替换成“mirGarden”
        sed -i "s/garden/mirGarden/g" 1.txt
        将当前目录下的所有文件内的“garden”替换成“mirGarden” 
            sed -i "s/garden/mirGarden/g" 

    敞亮的心!
  • 相关阅读:
    [bzoj3524]Couriers
    [bzoj2789]Letters
    [bzoj4318]OSU!
    [luogu4570]元素
    [hdu6600]Just Skip The Problem
    [bzoj5025]单调上升路径
    [bzoj4557]侦察守卫
    [hdu5379]Mahjong tree
    [bzoj2957]楼房重建
    [noi253]A
  • 原文地址:https://www.cnblogs.com/duzhonglin/p/5608066.html
Copyright © 2011-2022 走看看