Delete blank spaces and blank lines in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Delete blank spaces and blank lines in a file
# 8  
Old 02-10-2015
Quote:
Originally Posted by systemoper
Hi Gurus,
Somebody can say me how to delete blank spaces and blank lines in a file unix, please.

Thank you for advanced.
We do not seem to be getting anywhere with this thread. And, it is obvious that we have a language barrier.

We do not understand what you are trying to do. The term "blank spaces" is confusing. The standards define <space> characters, <blank> characters (which includes <space> and <horizontal-tab> characters), <blank lines> (which are lines that contain zero or more <blank>s and a line terminating <newline> character), and <empty lines> (which are lines that just contain the line terminating <newline> character).

In post #6 in this thread you said "I need to back-spaces removed". But, <backspace> characters are not <blanks> and <backspace> characters do not appear on <blank lines>.

What are you trying to do?
  1. Do you want to remove every <space> character from a file?
  2. Do you want to remove every <horizontal-tab> character from a file?
  3. After removing every <space> and <horizontal-tab> character, do you want to remove every empty line from a file?
  4. Do you want to remove every <backspace> character from a file?
  5. Are there other characters you want to remove from a file?
How do you want to do it?
  1. Do you want to print a file to paper and cut these characters out with scissors?
  2. Do you want to make these changes using an interactive editor (such as vi)?
  3. Do you want to make these changes using a non-interactive editor (such as sed)?
  4. Do you want to make these changes using a tool in a pipeline (such as tr)?
Please give us some details about what you are trying to do so we can help you!
# 9  
Old 02-18-2015
If its a text file and you want to remove blank lines, the quickest way to do this is

cat file |egrep -v '^$'

You need "egrep" but its probably installed.
# 10  
Old 02-18-2015
Quote:
Originally Posted by riacovino
If its a text file and you want to remove blank lines, the quickest way to do this is

cat file |egrep -v '^$'

You need "egrep" but its probably installed.
No. This command will remove empty lines; not blank lines. And, you don't need egrep (or the currently preferred form grep -E), for this, plain:
Code:
grep -v '^$' file

will remove empty lines from file. The command:
Code:
grep -v '^[[:blank:]]*$' file

will remove blank lines from file.

And, there is no need to use cat here. Using cat takes more system resources and takes longer to get the results you want.
# 11  
Old 02-18-2015
Another nice way to remove blank lines from a file is :
Code:
awk NF file

Although that will also not work if there are backspace characters (?) on that line.
@OP could you post a sample?

Last edited by Scrutinizer; 02-18-2015 at 11:17 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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. Shell Programming and Scripting

Reform Lines in File without blank lines and spaces

Hello All, I have a file with data as below. Each line consists of 21 fields. I am not able to load them back to the database. 50733339,"834","834 ","005010X279A1","N","Y","007977163","0001 ",30,"2110D ","EB ","EB007 ","2 ","Conditional Required Data Element Miss ing... (3 Replies)
Discussion started by: Praveenkulkarni
3 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

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

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

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

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

9. Shell Programming and Scripting

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 (6 Replies)
Discussion started by: fedora
6 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