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$
  • 相关阅读:
    curl常用选项
    cuda
    mysql 备份文件.xbstream 恢复到本地
    firewall 常用命令(update...)
    ownCloud 研究笔记(update...)
    V3
    English trip EM3-LP-3A ROOMMATES Teacher:Corrine
    V3
    English trip EM3-LP-5A Shopping Teacher:Taylor
    新概念 Lesson 11 Which book?
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13837282.html
Copyright © 2011-2022 走看看