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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete blank lines, if blank lines are more than one using shell
# 1  
Old 05-10-2011
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
Code:
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

I need the output as below
Code:
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

Pls help me to get the desired output.

To be simple : I want to delete blank lines,if blank lines are more than one.

Thanks in advance

Anil.G

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 05-10-2011 at 07:42 AM.. Reason: code tags
# 2  
Old 05-10-2011
Code:
 sed '/./,/^$/!d' infile

should work for you...
This User Gave Thanks to Tytalus For This Post:
# 3  
Old 05-10-2011
Thank you Mr , Tytalus

Great day ahead
Anil.G
# 4  
Old 05-10-2011
try this

Code:
 cat testfile.txt |sed  '/^$/d' | sed s/$/\\n/

This User Gave Thanks to palanisvr For This Post:
# 5  
Old 05-10-2011
Hi Palani
Thanks it working .

Its really a good site. I just love this site.

Rgds
Anil.G
# 6  
Old 05-10-2011
Code:
cat -s urfile

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

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. 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

4. 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

5. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

6. Shell Programming and Scripting

Why cant i delete blank lines?

I have a sed pipeline: myVar=$(cat $FILE | sed -n '/regex/,/regex/{/regex/d;p}' | sed -n '/regex/!p' | sed -e s/*:// | sed /regex/,+8d \ ) sed '/^$/d' sed '/./!d' And i've tried to add that in a different order rather then just on the end..Why isnt it deleting all the blank... (2 Replies)
Discussion started by: omgsomuchppl
2 Replies

7. UNIX for Dummies Questions & Answers

delete traling blank lines only

Hi, I have blank line in the file, I just want to remove trailing blank lines. There are some blank lines between the lines, i don't want remove those. Just want to delete blank lines at the end. I used this command sed '/^$/d' infile > outfile, but it is not removing anything. I think... (1 Reply)
Discussion started by: visu
1 Replies

8. 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

9. Shell Programming and Scripting

delete blank lines with sed

I have a text file with blank lines fullfilled with spaces and others only containing the "Enter" caracter, the \012. I would like to eliminate all them with the sed command. Is it possible? making: sed '/^$/d' <file should delete the blank lines but doesn't work for the lines that only... (2 Replies)
Discussion started by: tmxps
2 Replies

10. UNIX for Dummies Questions & Answers

delete blank lines or lines with spaces only

hi there i'm trying to delete blank lines and or lines with spaces only from a series of files in an directory. to do so, i'm using this: for files in `ls /users/myname/pesop* 2>/dev/null` do grep -v ^$ $files > newfile mv newfile $files done now, this works great for blank lines but... (3 Replies)
Discussion started by: vascobrito
3 Replies
Login or Register to Ask a Question