sed/awk: Three consecutive blank lines in a file, how to delete two of them?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
sed/awk: Three consecutive blank lines in a file, how to delete two of them?
I've spent a stupidly long amount of time trying to figure this out. I think at this point I'm losing braincells instead of gaining knowledge.
Like the subject says, I have a file with three consecutive blank lines. I want to delete two and keep one.
Also, if anyone could direct me towards a guide on regular expressions particularly as they apply to sed, I would be grateful. I am having a hell of a time figuring out the syntax.
# delete all CONSECUTIVE blank lines from file except the first; also
# deletes all blank lines from top and end of file (emulates "cat -s")
sed '/./,/^$/!d' # method 1, allows 0 blanks at top, 1 at EOF
sed '/^$/N;/\n$/D' # method 2, allows 1 blank at top, 0 at EOF
Sed - Regular expressions
Feed your search engine with Sed and Regular Expression, you get a ton of hits.
If a book is your thing then I would suggest O'Reilly's Sed & AWK.
hmmm ... so your pointing out that you didn't read my post!!
In either post do I talk about setting user defined variables? No
If you care to review the awk manuals easily found on the net you will see that the awk internal variables, such as FS or RS, can be set post the
action and pattern string, eg:
Code:
awk '{print NF} FS=":" file
This has the effect of setting the field separator which can be done pre action / pattern statement with -F switch or included inside the statement
using the BEGIN{} setting.
Hopefully this will help you learn a little more about awk.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.