Re: Getting a text file rid of all superfluous blank lines

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 2005-12-03 at 11:54, Paul Smith wrote:
> > After this runs (takes no more than 10 seconds), you will
> > have a program named "noblank" in the current directory.
> > Either move it to a place in your path, or use ./noblank
> > to run it. The usage is:
> >
> > $ noblank < input_file > output_file
> 
> I kindly thank you, Mike, for your program. I have tried that, but when I run
> 
> $./noblank file1.txt file2.txt
> 
> the program does not terminate, as if waiting for something.

OK, I looked up the holding space syntax for sed.  This
command will do it in one pass:

sed -e '/^\s*$/{
N
/^\s*\n\s*$/D
}'  input_file >output_file

The first line finds matches from the beginning of a
line (^) followed by any amount of white space (\s)
which could also be represented as [ 	] with a
space an tab typed between the character-class brackets
but it is harder to type a tab on the shell command
line.  The number represented by * can be 0 or more
up to the end of the line ($).  When a match is found,
the N command appends the next line to the pattern
space where the match is repeated, allowing for the
embedded newline (\n) from the previous line and if
it succeeds, the D command deletes up to the embedded
newline.

If you expect to do it often, you can put the sed command
(inside the 's) in a file and execute it with
sed -f script_file
instead of typing it again.

It's rarely worth the trouble to compile a specialized
program for anything that can be done with regular
expression since the native unix tools are so good
at text manipulation.  If you can describe the problem
in words, you can usually come up with a sequence of
steps to transform what you have into what you want
with regular expression substitions. 

-- 
  Les Mikesell
   lesmikesell@xxxxxxxxx



[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux