til icon indicating copy to clipboard operation
til copied to clipboard

How can I replace multiple empty lines with a single empty line in bash?

Open xluffy opened this issue 3 years ago • 0 comments

Tự dưng có một file, sau khi được xử lý với sed để xoá đi dòng match với pattern và 3 dòng sau dòng đó. Xoá kiểu đó thì xài vầy:

gsed -i -e '/delete_me_and_3_lines_after/,+3d' til.txt

Một file 5_000 nghìn dòng, xong tự dưng dư ra mấy cái blank line khá khó chịu, trong khi muốn mỗi block sẽ cách nhau chỉ 1 blank line. Search lung tung thử với sed, grep, awk hoá ra đơn giản chỉ cần xài cat -s. 😳

For BSD-derived systems (including GNU):

You just need cat with the -s option which causes it to remove repeated empty lines from its output:

cat -s

From man page: -s --squeeze-blank: suppress repeated empty output lines.

xluffy avatar Oct 20 '21 15:10 xluffy