• linux 系统中awk 字符串处理函数


    1、sub、gsub

    root@DESKTOP-1N42TVH:/home/test# ls
    test.txt
    root@DESKTOP-1N42TVH:/home/test# cat test.txt  ## 测试数据
    a 3 3
    s 1 j
    z c m
    q e i
    3 4 k
    h f 3
    root@DESKTOP-1N42TVH:/home/test# awk '{sub(3,"xxx"); print $0}' test.txt    ## sub替换第一个匹配的字符
    a xxx 3
    s 1 j
    z c m
    q e i
    xxx 4 k
    h f xxx
    root@DESKTOP-1N42TVH:/home/test# awk '{gsub(3,"xxx"); print $0}' test.txt  ## gsub替换所有匹配的字符
    a xxx xxx
    s 1 j
    z c m
    q e i
    xxx 4 k
    h f xxx

    2、length

    root@DESKTOP-1N42TVH:/home/test# cat test.txt
    a 3 3 a 3 3
    s 1 j s 1 j
    z c sm z c m
    q e i388 q e i
    3 4 k33 3 4 k
    h f 3 h f 3
    root@DESKTOP-1N42TVH:/home/test# awk '{print length($0)}' test.txt
    11
    11
    12
    14
    13
    11
    root@DESKTOP-1N42TVH:/home/test# awk '{print length($3)}' test.txt
    1
    1
    2
    4
    3
    1
    root@DESKTOP-1N42TVH:/home/test# awk 'length($3) == 3' test.txt
    3 4 k33 3 4 k

    3、index / match

    root@DESKTOP-1N42TVH:/home/test# ls
    test.txt
    root@DESKTOP-1N42TVH:/home/test# cat test.txt
    a 3 3 a 3 3
    s 1 j s 1 j
    z c sm z c m
    q e i388 q e i
    3 4 k33 3 4 k
    h f 3 h f 3
    root@DESKTOP-1N42TVH:/home/test# awk '{print match($0, "j")}' test.txt
    0
    5
    0
    0
    0
    0
    root@DESKTOP-1N42TVH:/home/test# awk '{print index($0, "j")}' test.txt
    0
    5
    0
    0
    0
    0

    4、substr

    root@DESKTOP-1N42TVH:/home/test# ls
    test.txt
    root@DESKTOP-1N42TVH:/home/test# cat test.txt
    a 3 3 a 3 3
    s 1 j s 1 j
    z c sm z c m
    q e i388 q e i
    3 4 k33 3 4 k
    h f 3 h f 3
    root@DESKTOP-1N42TVH:/home/test# awk '{print substr($3, 1, 3)}' test.txt
    3
    j
    sm
    i38
    k33
    3
    root@DESKTOP-1N42TVH:/home/test# awk '{print substr($3, 1, 2)}' test.txt
    3
    j
    sm
    i3
    k3
    3
    root@DESKTOP-1N42TVH:/home/test# awk '{print substr($3, 2)}' test.txt
    
    
    m
    388
    33

    5、split

    root@DESKTOP-1N42TVH:/home/test# ls
    test.txt
    root@DESKTOP-1N42TVH:/home/test# cat test.txt
    test3_1_clean.fq.gz test3_2_clean.fq.gz
    test4_1_clean.fq.gz test4_2_clean.fq.gz
    test5_1_clean.fq.gz test5_2_clean.fq.gz
    root@DESKTOP-1N42TVH:/home/test# awk '{split($1, x, "_"); print x[1], x[2], $0}' test.txt
    test3 1 test3_1_clean.fq.gz test3_2_clean.fq.gz
    test4 1 test4_1_clean.fq.gz test4_2_clean.fq.gz
    test5 1 test5_1_clean.fq.gz test5_2_clean.fq.gz
  • 相关阅读:
    Flex布局
    【北京法院网上立案工作办法】
    欢迎注册北京法院电子诉讼平台账号
    授权书范本
    拷贝构造函数与移动构造函数
    力扣刷题计划-字符串-最长公共前缀
    多节点树结构
    WPF的TreeView添加鼠标双击事件MouseDoubleClick执行两次
    力扣刷题计划-动态规划-整数拆分
    Java解决字节流乱码问题
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15760644.html
Copyright © 2020-2023  润新知