regex to delete multiple blank lines in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regex to delete multiple blank lines in a file?
# 1  
Old 10-11-2007
regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this
#cat file
1

2

3



4

5
6
-

What I want is
1

2

3

4

5
6
--

any idea?
# 2  
Old 10-11-2007
Code:
sed '/^$/d;G' myFile

# 3  
Old 10-11-2007
thanks for the answer, but by doing this, you also ADD a empty line between
5 and 6, which before has no empty line between.

5
6

not
5

6



Quote:
Originally Posted by vgersh99
Code:
sed '/^$/d;G' myFile

# 4  
Old 10-11-2007
Code:
awk 1 RS= ORS="\n\n" filename

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 10-11-2007
Tjhis is awesome!

Can you explain a little bit what the argument "1" does here?

Quote:
Originally Posted by radoulov
Code:
awk 1 RS= ORS="\n\n" filename

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 6  
Old 10-11-2007
Sure:

everything <>0 in awk is true (you can use whatever you want: 1,2 ... 42 ...), true means use default action, which is print the current record.

You could use this, if you prefer:

Code:
awk -- -42 RS= ORS="\n\n" filename

# 7  
Old 10-11-2007
Thanks again! SmilieSmilieSmilieSmilie

Quote:
Originally Posted by radoulov
Sure:

everything <>0 in awk is true (you can use whatever you want: 1,2 ... 42 ...), true means use default action, which is print the current record.

You could use this, if you prefer:

Code:
awk -- -42 RS= ORS="\n\n" filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

Tried many options but unable to delete blank lines from text file

Hi, I tried the following options but was unable to delete blank lines from file Input file = temp.hash.txt temp.hash.txt content 90 0 89.56 0 0 57575.4544 56.89 (9 Replies)
Discussion started by: uuuunnnn
9 Replies

3. Shell Programming and Scripting

Grepping or awking multiple lines in a file - regex

data.txt: hellohellohello mellomello1mello tellotellotellotello bellobellowbellow vellow My attempts: egrep ".*mello1\n.*bellow" data.txt awk '/.*mello1.*\nbellow/' data.txt how can i search for patterns that are on different lines using simple egrep or awk? i only want the... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

5. Shell Programming and Scripting

Delete blank lines in a file

Hi All, I have a file and I need to delete the lines that are blank and is starting with some characters below. Something like below: Regular Ascii File: Line1: AGODA1 BUSAN||SK Lord Beach 4/6/2012 4/7/2012 68060 Line2: AGODA2 BUSAN||SK Beach Hotel 4/6/2012 4/7/2012 610200 Line3: ... (4 Replies)
Discussion started by: rkumar28
4 Replies

6. Shell Programming and Scripting

Need sed help: find regex and if the next next line is blank, delete both

I've got a report I need to make easier to read Using sh on HP-UX 11.12. In short, I want to search for a regular expression and when found, examine the next line to see if it's blank. If so, then delete both lines. If not blank, move on to the next regexp. Repeat. So far I've got: ... (7 Replies)
Discussion started by: Scottie1954
7 Replies

7. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

8. Shell Programming and Scripting

Delete blank lines from a file

Hi, I want to use diff to compare two files in a Perl file. But one of the files has some blank lines at the end. So I want to delete the blank lines from the file firstly and then use diff to compare them. But I dont know how to delete the blank lines from the files. Meanwhile, the system is... (5 Replies)
Discussion started by: Damon_Qu
5 Replies

9. UNIX for Dummies Questions & Answers

delete blank lines from a file

can anyone show me how to delete blank lines from a file. thanks in advance (2 Replies)
Discussion started by: sachin.gangadha
2 Replies

10. Shell Programming and Scripting

Delete blank lines at the end of file

I am attempting to delete blank lines in my file and I've used this command: sed '/^$/d' $file > $file.fixed all this seems to do is copy the file and not delete the blank lines located at the end of the file. Any assistance would be greatly appreciated. (3 Replies)
Discussion started by: TL56
3 Replies
Login or Register to Ask a Question