zoukankan      html  css  js  c++  java
  • linux系统中在指定行后添加空行

    1、在所有行后添加空行

    [root@centos79 test]# cat a.txt
    a g r e
    i x k like
    a f g liker
    s t 2 a
    b d s i
    [root@centos79 test]# awk '{print $0 "
    "}' a.txt
    a g r e
    
    i x k like
    
    a f g liker
    
    s t 2 a
    
    b d s i

    2、

    [root@centos79 test]# cat a.txt
    a g r e
    i x k like
    a f g liker
    s t 2 a
    b d s i
    [root@centos79 test]# awk '{if(NR%2 == 0){print $0 "
    "}else {print $0}}' a.txt  ## 偶数后添加空行
    a g r e
    i x k like
    
    a f g liker
    s t 2 a
    
    b d s i
    [root@centos79 test]# awk '{if(NR%2 == 0){print "
    "$0}else {print $0}}' a.txt  ## 偶数前添加空行
    a g r e
    
    i x k like
    a f g liker
    
    s t 2 a
    b d s i

    3、

    [root@centos79 test]# cat a.txt
    a g r e
    i x k like
    a f g liker
    s t 2 a
    b d s i
    [root@centos79 test]# awk '{if($0 ~ /f/){print $0 "
    "} else {print $0}}' a.txt  ## 利用正则
    a g r e
    i x k like
    a f g liker
    
    s t 2 a
    b d s i
    [root@centos79 test]# awk '{if($0 ~ /g/){print $0 "
    "} else {print $0}}' a.txt  ## 利用正则
    a g r e
    
    i x k like
    a f g liker
    
    s t 2 a
    b d s i

    4、

    [root@centos79 test]# cat a.txt
    a g r e
    i x k like
    a f g liker
    s t 2 a
    b d s i
    [root@centos79 test]# awk 'BEGIN{printf "
    "}{print $0}' a.txt  ##行首添加空行
    
    a g r e
    i x k like
    a f g liker
    s t 2 a
    b d s i
    [root@centos79 test]# awk '{print $0}END{printf "
    "}' a.txt  ## 行尾添加空行
    a g r e
    i x k like
    a f g liker
    s t 2 a
    b d s i
    
    [root@centos79 test]# awk 'BEGIN{printf "
    "}{print $0}END{printf "
    "}' a.txt  ## 行首、行尾同时添加空行
    
    a g r e
    i x k like
    a f g liker
    s t 2 a
    b d s i
  • 相关阅读:
    python中创建函数和调用函数
    python中函数的参数
    python 函数中的关键字参数
    python中创建集合
    python中函数文档
    python中函数形式参数、实际参数、位置参数、关键字参数
    python中不可变集合
    我是谁?(程序员版)
    《林阴小路》疏辨
    用户接口常用术语英语德语对照
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14970256.html
Copyright © 2011-2022 走看看