LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 11-28-2005, 04:49 PM   #1
human2.0
LQ Newbie
 
Registered: Aug 2005
Location: UK
Distribution: Mandriva 2005LE
Posts: 7

Rep: Reputation: 0
Deleting empty line at end of text file in BASH


I have been attempting to remove empty lines from a text file. I've been using sed with:
Code:
sed '/^ *$/d' [file1] > [file2]
I then mv file 2 on to file 1. The problem with this approach is that as it is written to the terminal a new line is written to the end of file2, thus defeating the purpose of the line removal. I can't think of any other way round this issue.

Thanks in advance for any pointers.
 
Old 11-28-2005, 05:30 PM   #2
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 63
Are you sure there's an empty line at the end? Some text editors add an extra newline at the end when you open a file. If you cat file2 in a terminal does it have a blank line at the end?
 
Old 11-28-2005, 05:35 PM   #3
human2.0
LQ Newbie
 
Registered: Aug 2005
Location: UK
Distribution: Mandriva 2005LE
Posts: 7

Original Poster
Rep: Reputation: 0
Cat doesn't show up an extra line but with a file with no blank lines in before an extra byte is added to the filesize. The carriage return at the end is the only possible change.
 
Old 06-29-2006, 10:59 PM   #4
tmaxx
LQ Newbie
 
Registered: Jun 2006
Posts: 21

Rep: Reputation: 15
just wanna thank you the sed code posted above.. helped be so much
 
Old 05-08-2007, 04:50 AM   #5
bman777
LQ Newbie
 
Registered: May 2007
Distribution: Fedora Core
Posts: 1

Rep: Reputation: 0
Smile

Tip : if you don't want to generate a new file and do this inline, add -i

sed -i '/^ *$/d' [file1]
 
Old 05-08-2007, 10:00 AM   #6
dasy2k1
Member
 
Registered: Oct 2005
Location: 127.0.0.1
Distribution: Ubuntu 12.04 X86_64
Posts: 954

Rep: Reputation: 34
i thought that all Unix compliant text fies had to have a newline at the end
 
Old 03-27-2009, 07:39 PM   #7
chrysler
LQ Newbie
 
Registered: Mar 2009
Location: Taiwan
Distribution: ubuntu
Posts: 3

Rep: Reputation: 0
Question variant of "Deleting empty line at end of text file in BASH"

There is a variant of problem about "Deleting empty line at end of text file in BASH".

If I want to reduce multiple continuous blank lines to just one blank line, how can I do? In other words, I don't want to eliminate just one blank line, but I want to reduce two or more continues blank lines to just one blank line. BTW, the blank lines might include space characters.
 
Old 03-27-2009, 08:34 PM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by chrysler View Post
There is a variant of problem about "Deleting empty line at end of text file in BASH".

If I want to reduce multiple continuous blank lines to just one blank line, how can I do? In other words, I don't want to eliminate just one blank line, but I want to reduce two or more continues blank lines to just one blank line. BTW, the blank lines might include space characters.
This will do it for completely blank lines (not if they contain spaces, however):
Code:
perl -00 -pe '' <file-name>
This will work even if there are spaces or tabs in one of the successive blank lines. (I'm sure that there are more elegant ways to do this second stage, but I'm tired, so this was the best I could do right now.)
Code:
#!/usr/bin/env perl
use strict;
use warnings;

my $blank;

while (<>) {
  if (m/\S/) {
    print;
    $blank = 0;
  }
  elsif ($blank == 1) {
    next;
  }
  else {
    print;
    $blank = 1;
  }
}
Save it as, say, reducer and run it as perl reducer <file-name>.

Edit: this Awk one-liner appears to work for all cases, so it's better.
Code:
awk 'NF { print $0 "\n" }' <file-name>
Edit 2: Now to round this out, here's the Perl one-liner that does what that very elegant Awk one-liner does. (I found the Awk one-liner here.) Basically, it says, if I can split the line, print it and a newline. Otherwise, move on. It means, no more than one new line between lines with text. This does what you wanted. I'm leaving all the intermediate steps as a reminder to myself not to let go of things.
Code:
 perl -nle 'print "$_\n" if split' <file-name>

Last edited by Telemachos; 03-27-2009 at 09:41 PM.
 
1 members found this post helpful.
Old 04-01-2009, 02:44 AM   #9
chrysler
LQ Newbie
 
Registered: Mar 2009
Location: Taiwan
Distribution: ubuntu
Posts: 3

Rep: Reputation: 0
When I tried Telemachos' schemes, the schemes have a significant problem. A blank line will be inserted if there is no blank line between lines of original text.
 
  


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
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 09:42 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
bash programming-append single line to end of file pheasand Linux - General 3 01-12-2006 06:59 PM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
Empty terminal [command line] with no text after kernel upgrade to 2.6.7 whencat Red Hat 7 07-09-2004 03:59 PM


All times are GMT -5. The time now is 09:58 PM.

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
Open Source Consulting | Domain Registration