zoukankan      html  css  js  c++  java
  • linux系统中awk命令删除行首行尾的空格和制表符

    1、测试数据

    [root@linuxprobe test]# ls   ## 测试数据
    a.txt
    [root@linuxprobe test]# cat a.txt
      fd gfd fgd
    fsdafds
                    dfs
                    sfa
    [root@linuxprobe test]# cat -A a.txt
      fd gfd fgd^I^I$
    fsdafds       $
    ^I^Idfs$
      ^I^Isfa^I  $
    [root@linuxprobe test]# sed -n l a.txt
      fd gfd fgd\t\t$
    fsdafds       $
    \t\tdfs$
      \t\tsfa\t  $

    2、删除行首空格和制表符

    [root@linuxprobe test]# awk '{sub(/^[\t ]*/,"");print}' a.txt
    fd gfd fgd
    fsdafds
    dfs
    sfa
    [root@linuxprobe test]# awk '{sub(/^[\t ]*/,"");print}' a.txt | cat -A
    fd gfd fgd^I^I$
    fsdafds       $
    dfs$
    sfa^I  $
    [root@linuxprobe test]# awk '{sub(/^[\t ]*/,"");print}' a.txt | sed -n l
    fd gfd fgd\t\t$
    fsdafds       $
    dfs$
    sfa\t  $

    3、awk命令删除行位空格和制表符

    [root@linuxprobe test]# cat -A a.txt
      fd gfd fgd^I^I$
    fsdafds       $
    ^I^Idfs$
      ^I^Isfa^I  $
    [root@linuxprobe test]# sed -n l a.txt
      fd gfd fgd\t\t$
    fsdafds       $
    \t\tdfs$
      \t\tsfa\t  $
    [root@linuxprobe test]# awk '{sub(/[\t ]*$/,"");print}' a.txt
      fd gfd fgd
    fsdafds
                    dfs
                    sfa
    [root@linuxprobe test]# awk '{sub(/[\t ]*$/,"");print}' a.txt | cat -A
      fd gfd fgd$
    fsdafds$
    ^I^Idfs$
      ^I^Isfa$
    [root@linuxprobe test]# awk '{sub(/[\t ]*$/,"");print}' a.txt | sed -n l
      fd gfd fgd$
    fsdafds$
    \t\tdfs$
      \t\tsfa$

    4、同时删除行首行尾空格和制表符

    [root@linuxprobe test]# awk '{sub(/^[\t ]*|[\t ]*$/,"");print}' a.txt
    fd gfd fgd
    fsdafds
    dfs
    sfa
    [root@linuxprobe test]# awk '{sub(/^[\t ]*|[\t ]*$/,"");print}' a.txt | cat -A
    fd gfd fgd^I^I$
    fsdafds       $
    dfs$
    sfa^I  $
    [root@linuxprobe test]# awk '{gsub(/^[\t ]*|[\t ]*$/,"");print}' a.txt | cat -A  ## 用sub替换gsub
    fd gfd fgd$
    fsdafds$
    dfs$
    sfa$
  • 相关阅读:
    UVA 125 Numbering Paths
    UVA 515 King
    UVA 558 Wormholes
    UVA 10801 Lift Hopping
    UVA 10896 Sending Email
    SGU 488 Dales and Hills
    HDU 3397 Sequence operation
    数据库管理工具navicat的使用
    javascript封装animate动画
    关于webpack没有全局安装下的启动命令
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13837282.html
Copyright © 2011-2022 走看看