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
  • 相关阅读:
    在一个很长的字符串中搜索自定义字符串的问题(通过多线程实现)
    老鼠走迷宫
    js控制父子页面传值(iframe和window.open)
    C#后台跳转
    CSS小技巧-图片自动缩放
    js中去除换行( )
    js去除首尾空格
    JQuery隔行变色
    Web开发在线工具
    JQuery标签去重与数组去重
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15463315.html
Copyright © 2011-2022 走看看