Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

linux - Add blank line between lines from different groups

I have a file A of the form (frequency,file name,code lines):

1 file_name1 code_line1
2 file_name2 code_line2
2 file_name2 code_line3
2 file_name3 code_line4
2 file_name3 code_line5
3 file_name4 code_line6
3 file_name4 code_line7
3 file_name4 code_line8

I want output B as:

1 file_name1 code_line1

2 file_name2 code_line2
2 file_name2 code_line3

2 file_name3 code_line4
2 file_name3 code_line5

3 file_name4 code_line6
3 file_name4 code_line7
3 file_name4 code_line8

Basically the file A contains file name and code lines from the file and the first field is the frequency, i.e., number of code lines in the file.

I am supposed to go through these code lines file wise. I am finding it tedious and it would be easier for me if there was a line gap between entries of different files, hence the desired output.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Awk could do it:

awk '{if(NR > 1 && $2 != prev_two){printf "
";} prev_two=$2; print $0}' A

A being the file name.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...