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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.