zoukankan      html  css  js  c++  java
  • linux系统中对指定行的字符串进行替换

    1、测试数据

    [root@PC3 test]# cat a.txt
    e r e y e u e
    e e g e 3 h r
    1 3 e g e y e
    e s e e e e e
    e t s t e s r
    d g e s w t e
    [root@PC3 test]# cat b.txt
    1
    3
    5

    2、将1、3、5行中的e替换为x

    [root@PC3 test]# cat a.txt
    e r e y e u e
    e e g e 3 h r
    1 3 e g e y e
    e s e e e e e
    e t s t e s r
    d g e s w t e
    [root@PC3 test]# cp a.txt a.txt.bak
    [root@PC3 test]# cat b.txt
    1
    3
    5
    [root@PC3 test]# cat b.txt | while read i; do sed -i "$i s/e/x/g" a.txt; done
    [root@PC3 test]# cat a.txt
    x r x y x u x
    e e g e 3 h r
    1 3 x g x y x
    e s e e e e e
    x t s t x s r
    d g e s w t e

    3、awk

    [root@PC3 test]# cat a.txt
    e r e y e u e
    e e g e 3 h r
    1 3 e g e y e
    e s e e e e e
    e t s t e s r
    d g e s w t e
    [root@PC3 test]# cat b.txt
    1
    3
    5
    [root@PC3 test]# cp a.txt a.txt.bak
    [root@PC3 test]# for i in $(cat b.txt ); do awk -v a=$i '{if(NR == a) {gsub("e","x");print $0} else {print $0}}' a.txt > a && mv a a.txt; done
    [root@PC3 test]# cat a.txt
    x r x y x u x
    e e g e 3 h r
    1 3 x g x y x
    e s e e e e e
    x t s t x s r
    d g e s w t e
  • 相关阅读:
    了解WP的传感器
    载入条LoadingBar
    能分组的GridView
    ASP.NET MVC的过滤器
    ASP.NET的路由
    自己绘制的仪表盘
    可拖拽的ListBox
    自己绘制的滑块条
    利用mciSendString播放音频
    mis导入器的加强版——vdproj文件资源浏览器
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15054930.html
Copyright © 2011-2022 走看看