zoukankan      html  css  js  c++  java
  • linux系统中如何删除空行

    1、测试数据

    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# cat -A a.txt
    1 d f$
    $
    2 3 f$
    3 s 8$
    $
    $
    4 f a$
    d g 8$

    (删除完全的空行, 不包含空格和制表符)

    2、sed

    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# sed '/^$/d' a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8
    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# sed -n '/./p' a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8

    2、grep

    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# grep -v "^$" a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8
    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# grep "^." a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8

    3、awk

    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# awk '$1 ~ /./ {print $0}' a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8
    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# awk '/./ {print $0}' a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8
    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# awk '$0 != ""' a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8

    包含空格或支付表的情况。

    1、测试数据

    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# sed -n l a.txt
    1 d f$
    $
    2 3 f$
    3 s 8$
       $
    \t\t$
    4 f a$
    d g 8$

    2、sed

    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# sed '/^[\t ]*$/d' a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8
    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# sed '/^\s*$/d' a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8

    2、awk

    root@PC1:/home/test/test/test# cat a.txt
    1 d f
    
    2 3 f
    3 s 8
    
    
    4 f a
    d g 8
    root@PC1:/home/test/test/test# awk NF a.txt
    1 d f
    2 3 f
    3 s 8
    4 f a
    d g 8
  • 相关阅读:
    npm显示已安装的包
    webpack安装以及一些配置
    Chrome扩展之css used 获取网页样式
    搜索引擎高级用法
    C 语言-HelloWorld
    首先不谈C语言,我们先来谈谈编程工具
    HTTP content-type
    HTTP 响应头消息
    HTTP状态码
    HTTP请求方法
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15463315.html
Copyright © 2011-2022 走看看