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 "\n"}' 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 "\n"}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 "\n"$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 "\n"} 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 "\n"} 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 "\n"}{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 "\n"}' 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 "\n"}{print $0}END{printf "\n"}' a.txt  ## 行首、行尾同时添加空行

a g r e
i x k like
a f g liker
s t 2 a
b d s i

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM