Can't remove blank lines from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can't remove blank lines from a file
# 8  
Old 02-27-2010
Bug Remove Blank Lines

Dear Friend,

You can use the following code, to delete the blank lines from a file

The file contains the following content
welcome1

welcome2

welcome3

Code:
 
sed -i -e '/^$/D'  file

After execute the above code, the file content is
welcome1
welcome2
welcome3

Here -i is used to affect the file
# 9  
Old 02-27-2010
You use the following command to remove the blank lines from a file.

sed -i '/^$/d' filename

d - This is used to clear the content of the pattern space.
D - This is used to remove the first line in the pattern space.Not all the contents.
So,d is preferable than D.
# 10  
Old 02-27-2010
Code:
sed -i '/./!d' input file.

This also will remove the blank line from the input file
# 11  
Old 02-27-2010
Code:
grep . infile > outfile

# 12  
Old 02-27-2010
Quote:
Originally Posted by Corona688
Since this is cygwin another culprit may be the CPM-style carriage returns Windows text files have. Annoyingly, plenty of things can't actually match a carriage return.

Code:
tr -d '\r' < input | grep -v "^$" > output

My money's on Corona688 and carriage returns in the data. So far, all other suggestions in the thread are variations on attempts listed in the original post. If those failed, so will the suggestions.

Just my 2 cents Smilie

If Corona688's suggestion does not work, take a look at the file using od to determine if those blank lines contain an "invisible" byte other than \r :
Code:
od -c inputfile

Cheers,
Alister

Last edited by alister; 02-27-2010 at 02:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove Blank lines in VI

Hi, Which option is used to remove blank lines in VI (AIX). ? Regards, Siva (6 Replies)
Discussion started by: ksgnathan
6 Replies

2. Shell Programming and Scripting

How to remove blank lines in a file and save the file with same name?

I have a text file which has blank lines. I want them to be removed before upload it to DB using SQL *Loader. Below is the command line, i use to remove blank lines. sed '/^ *$/d' /loc/test.txt If i use the below command to replace the file after removing the blank lines, it replace the... (6 Replies)
Discussion started by: vel4ever
6 Replies

3. UNIX and Linux Applications

remove all blank lines

When I 'vi' my test file I see some blank lines. However once I do :set list to display hidden characters, I see the empty lines literally like this: ^I$ How do I remove them? I cannot find a regex to match them. (3 Replies)
Discussion started by: alexsuv
3 Replies

4. Shell Programming and Scripting

How to remove blank lines

Hi, I am facing a problem related to removing blank lines from a text document. Input Error 17-05-2011 11:01:15 VisualSVN Server 2.1 1001 The following information was included with the event: line3 line4 Error 17-05-2011 11:00:25 VisualSVN Server 2.1 ... (13 Replies)
Discussion started by: mayursingru
13 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

Remove blank lines

I really hope someone can help me with this. I have several php files from a forum that I run, that now for some reason have blank lines after every line. Is there an easy way to make a script that does the following: * If there are consecutive blank lines, delete all of them except one. * If... (9 Replies)
Discussion started by: KidCactus
9 Replies

7. UNIX for Dummies Questions & Answers

Remove blank lines and comments from text file

Hi, I am using BASH. How can I remove any lines in a text file that are either blank or begin with a # (ie. comments)? Thanks in advance. Mike (3 Replies)
Discussion started by: msb65
3 Replies

8. Shell Programming and Scripting

remove blank lines

I have joined 2 files. Join command worked fine. but the result showing extra blank lines. I tried to remove blank spaces by using awk (-- -42 RS= ORS="\n\n" file.txt) and sed (sed '/^ *$/d' file.txt)commands but didn't remove any Any suggestions plz:D 123 tab ....... ......tab .......234... (3 Replies)
Discussion started by: repinementer
3 Replies

9. UNIX for Dummies Questions & Answers

remove blank lines in *.srt file :)

Hi all, I use translate web to get subtitle file in my langues . But in output file have bad blank lines . I need scrip (i use debian ) to remove this blank lines . szintax of my bad *.srt file : ------ number blank1 number:number ---> number:number blank2 text1 . textn blankS... (10 Replies)
Discussion started by: hungbp
10 Replies

10. UNIX for Dummies Questions & Answers

Remove blank lines

¿How can i remove blank lines between all lines in a long text file? Example WrongFile.txt : Line 1 Line 2 Line 3 CorrectFile.txt : Line 1 Line 2 Line 3 Thanks in advance :confused: (4 Replies)
Discussion started by: osymad
4 Replies
Login or Register to Ask a Question