LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-16-2010, 12:16 PM   #1
recomboDNA
LQ Newbie
 
Registered: Jun 2008
Distribution: Arch
Posts: 13

Rep: Reputation: 1
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.
 
Old 06-16-2010, 12:24 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Sed one-liners

That link also has an answer to your question:
Quote:
# 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.

Hope this helps.
 
Old 06-17-2010, 05:04 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,169

Rep: Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776
If you have awk pointing at gawk, the following can work:
Code:
awk 'gsub(/\n\n\n/,"\n")' RS="\0" file
 
Old 06-17-2010, 07:35 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by grail View Post
If you have awk pointing at gawk, the following can work:
Code:
awk 'gsub(/\n\n\n/,"\n")' RS="\0" file
The part highlighted in bold won't work.

Code:
awk 'BEGIN { RS="\0" } { gsub(/\n\n\n/,"\n")}' file
 
Old 06-17-2010, 08:18 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,169

Rep: Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776
Quote:
The part highlighted in bold won't work.
I am guessing that is dependent on versions ... works just fine for me
You can also do other variables like FS after as well
 
Old 06-17-2010, 08:19 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I don't know, maybe it would work. Haven't tried it.
 
Old 06-17-2010, 09:04 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,169

Rep: Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776
Quote:
I don't know, maybe it would work. Haven't tried it.
Are you kidding me!! Please have your facts straight or at least try something before you say it doesn't work or is incorrect.
 
Old 06-17-2010, 09:11 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Code:
$ awk -v myvar='it works!' 'BEGIN {print myvar}' test.sh
it works!
$ awk myvar='it works!' 'BEGIN {print myvar}' test.sh
awk: cmd. line:1: myvar=it works!
awk: cmd. line:1:                ^ unexpected newline or end of string
$ awk 'BEGIN {print myvar}' myvar='it works!' test.sh

$ awk 'BEGIN {print myvar}' -v myvar='it works!' test.sh

$
 
Old 06-17-2010, 09:50 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,169

Rep: Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed: delete lines after last occurrence of a pattern in a file zugvogel Programming 4 11-17-2009 01:49 AM
sed delete lines from file one if regexp are listed in file two fucinheira Programming 6 09-17-2009 08:28 AM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM


All times are GMT -5. The time now is 05:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration