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

    2、将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
    [root@PC3 test]# awk '{for(i = 1; i <= NF; i++) if(i >= 3 && i <= 5 && $i == "e") {$i = "x"} {print $0}}' a.txt
    e r x y x u e
    e e g x 3 h r
    1 3 x g x y e
    e s x x x e e

    3、将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]# 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 '{for(i = 1; i <= NF; i++) if(i == a && $i == "e") {$i = "x"} {print $0}}' a.txt > a && mv a a.txt; done
    [root@PC3 test]# cat a.txt
    x r x y x u e
    x e g e 3 h r
    1 3 x g x y e
    x s x e x e e
    x t s t x s r
    d g x s w t e
  • 相关阅读:
    2020 camp day0 -F
    2020 camp day2 -k
    扫描线 hdu1542
    Assign the task HDU
    快速排序模板
    Java中Thread的常用方法
    Java中多线程的创建
    二分模板
    main()的简单理解
    单例模式饿汉式和懒汉式的实现
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15054924.html
Copyright © 2011-2022 走看看