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
  • 相关阅读:
    获取指定<文字行数>的<高度>是多少 TextKit
    JS 与 OC 交互
    PHP-note
    MySQL函数
    freeSWITCH之多平台测试通信
    freeSWITCH之安装
    PHP面向对象
    MySQL Optimization 优化原理
    理解常量指针与指针常量?
    InterView之PHP(2)
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15463315.html
Copyright © 2011-2022 走看看