Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

blank lines

0 views
Skip to first unread message

Kenneth A. Magrow

unread,
Nov 26, 1996, 3:00:00 AM11/26/96
to

hello--
I have a file say:
bob

pete
mary

joe
ron

Want file to look like:

bob
pete
mary
joe ron

I want to remove the blank line using a sed command but I can't seem to
get the right one. Any suggestions? Thanks.

Ken Magrow

Cal Dunigan

unread,
Nov 26, 1996, 3:00:00 AM11/26/96
to

On Tue, 26 Nov 1996, Kenneth A. Magrow wrote:
> I want to remove the blank line using a sed command but I can't seem to
> get the right one. Any suggestions? Thanks.

If those lines are empty it's easy:
grep -v '^$' infile

If there might be white space, control characters etc., the more
general solution is:
grep "[!-~]" infile

If you really want to use sed:
sed '/^$/d' infile or
sed -n "/[!-~]/p" infile

The secret in either case is the regular expressions, "^$" matches
only empty lines; "[!-~]" matches any single character in the range.
A quick look at the ascii table (man ascii) will show you that this
will match everything in the standard table that is "printable".

For more on REs type "man ed".

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Cal Dunigan c...@ccdi.com There is something very wrong with
Consulting a world where Ken Thompson lives in
Modeling relative obscurity and Bill Gates
Training is a famous billionaire.
//////////////////////////////////////////////////////////////////////


Neil Moore

unread,
Nov 27, 1996, 3:00:00 AM11/27/96
to

"Kenneth A. Magrow" <k-ma...@uiuc.edu> writes:

>
> hello--
> I have a file say:
> bob
>
> pete
> mary
>
>
> joe
> ron
>
> Want file to look like:
>
> bob
> pete
> mary
> joe ron
>

> I want to remove the blank line using a sed command but I can't seem to
> get the right one. Any suggestions? Thanks.

sed -e "/^$/d"

or, to remove blank lines *and* lines containing no alphanumerical
characters:

sed -e "/^\W*$/d"

or, to remove blank lines *and* lines consisting only of spaces

sed -e "/^ *$/d"

--
-Neil Moore
(finger amet...@170.180.106.55 for my Geek Code/Nethack Code)

harm b. j. ensing

unread,
Nov 27, 1996, 3:00:00 AM11/27/96
to

In article <329B4A...@uiuc.edu>, k-ma...@uiuc.edu says...

>
>hello--
> I have a file say:
>bob
>
>pete
>mary
>
>
>joe
>ron
>
>Want file to look like:
>
>bob
>pete
>mary
>joe ron
>
>I want to remove the blank line using a sed command but I can't seem to
>get the right one. Any suggestions? Thanks.
>
>Ken Magrow


That's easy (once you know it ofcourse) :

sed '/$^/d' file

Harm


Brian S Hiles

unread,
Nov 27, 1996, 3:00:00 AM11/27/96
to

harm b. j. ensing (h.b.j....@ptt-telecom.nl) didn't proof-read:
: That's easy (once you know it ofcourse) :
: sed '/$^/d' file

sed '/^$/d' file :-)

-Brian
--
,---. ,---. ,---. ,---. ,---. ,---. ,---.
/ _ \ / _ \ / _ \ / _ \ / _ \ / _ \ / _ \
.' / \ `.' / mailto:bsh2...@challenger.atc.fhda.edu \ `.' / \ `.
__,' `.___,' `.___,' `.___,' `.___,' `.___,' `.___,' `.__

era eriksson

unread,
Nov 28, 1996, 3:00:00 AM11/28/96
to

On Tue, 26 Nov 1996 13:51:07 -0600, "Kenneth A. Magrow"
<k-ma...@uiuc.edu> posted to comp.unix.shell:

> I want to remove the blank line using a sed command but I can't seem to
> get the right one. Any suggestions? Thanks.

On a tangential note, a useful idiom to learn is sort | uniq (or sort
-u, with many versions of sort). Then you often want to trash the
first line, tail +2 (with many versions of tail) or just grep -vx ''
to remove an empty line.

Hope this helps,

/* era */

> Want file to look like:

<...>
> joe ron

Let's just assume this was a typo? :-)

--
See <http://www.ling.helsinki.fi/~reriksso/> for mantra, disclaimer, etc.
* If you enjoy getting spam, I'd appreciate it if you'd register yourself
at the following URL: <http://www.ling.helsinki.fi/~reriksso/spam.html>

Heiner Steven

unread,
Dec 2, 1996, 3:00:00 AM12/2/96
to

Kenneth A. Magrow (k-ma...@uiuc.edu) wrote:

[...]


> I want to remove the blank line using a sed command but I can't
> seem to get the right one. Any suggestions? Thanks.

This is a non-sed solution:

grep . < INFILE > OUTFILE

Heiner
--
-------------------------------------------------------------
/ Heiner Steven h...@bintec.de / The expressed opinions are /
/ BinTec Communications / mine, not BinTec's -- /
/ Willstaetterstr. 30 ------- ...even if they should be ;-)/
/ D-90449 Nuernberg / http://www.bintec.de/english /
------------------------------------------------------------

Joseph Greene

unread,
Dec 4, 1996, 3:00:00 AM12/4/96
to

Kenneth A. Magrow (k-ma...@uiuc.edu) wrote:
>
> [...]
> > I want to remove the blank line using a sed command but I can't
> > seem to get the right one. Any suggestions? Thanks.
>

WARNING: Untested theory:

use the sed command s/^ *$//


Just a thought...may not even be a well fromed one....

Joe Greene
No-one but me owns my opinions.

James Hunt

unread,
Dec 5, 1996, 3:00:00 AM12/5/96
to

Kenneth A. Magrow (k-ma...@uiuc.edu) wrote:

> I want to remove the blank line using a sed command but I can't
> seem to get the right one. Any suggestions? Thanks.

How about:

sed '/^$/d'

James
___________________________
mailto:Jame...@lucent.com

Tim Haynes

unread,
Dec 9, 1996, 3:00:00 AM12/9/96
to

Kenneth A. Magrow (k-ma...@uiuc.edu) wrote:
: I want to remove the blank line using a sed command but I can't seem to
: get the right one. Any suggestions? Thanks.

Try awk, thusly:
cat mess | awk 'NF!=0 {print}'

Should do the job. There's a variant on a theme with a counter in it, that
reduces it to 1 blank line at once (good for man pages).

~Tim

__________ ________________
_______/Tim Haynes\__________________________________/Top...@ed.ac.uk\_______
|Graduate CS-type person from Edinburgh http://www.tardis.ed.ac.uk/~tdxh |
|GCS d? p c++++ l= e* m* s--/+ !n h+ f+ !g w+ t--- r- y? |
| Just think.. to a worm, digging a hole deeper than he is long, is more |
| beneficial than going fishing. |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


walter briscoe

unread,
Dec 13, 1996, 3:00:00 AM12/13/96
to

In article <E2670v.KsI.0.sta...@dcs.ed.ac.uk>
td...@tardis.ed.ac.uk "Tim Haynes" writes:

> Kenneth A. Magrow (k-ma...@uiuc.edu) wrote:
> : I want to remove the blank line using a sed command but I can't seem to
> : get the right one. Any suggestions? Thanks.
>
> Try awk, thusly:
> cat mess | awk 'NF!=0 {print}'

I guess Randal Schwartz would prefer: awk 'NF!=0 {print}' mess


>
> Should do the job. There's a variant on a theme with a counter in it, that
> reduces it to 1 blank line at once (good for man pages).
>
> ~Tim
>
> __________ ________________
> _______/Tim Haynes\__________________________________/Top...@ed.ac.uk\_______
> |Graduate CS-type person from Edinburgh http://www.tardis.ed.ac.uk/~tdxh |> |GCS d? p c++++ l= e* m* s--/+ !n h+ f+ !g w+ t--- r- y? |> | Just think.. to a worm, digging a hole deeper than he is long, is more |> |
beneficial than going fishing. |> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
>

I usually use: `sed -e '/^$/d' mess` to remove blank lines.
To remove "logically blank" lines, I might use:i
`sed -e '/^[ \t]*$/d' mess` if sed had a symbolic escape character
mechanism. Just replace "\t" by a tab character " "
--
Walter Briscoe


Randell D.

unread,
Dec 19, 1996, 3:00:00 AM12/19/96
to wal...@wbriscoe.demon.co.uk

walter briscoe wrote:
>
> In article <E2670v.KsI.0.sta...@dcs.ed.ac.uk>
> td...@tardis.ed.ac.uk "Tim Haynes" writes:
>
> > Kenneth A. Magrow (k-ma...@uiuc.edu) wrote:
> > : I want to remove the blank line using a sed command but I can't seem to
> > : get the right one. Any suggestions? Thanks.
> >
> > Try awk, thusly:
> > cat mess | awk 'NF!=0 {print}'
>
> I guess Randal Schwartz would prefer: awk 'NF!=0 {print}' mess
> >
> > Should do the job. There's a variant on a theme with a counter in it, that
> > reduces it to 1 blank line at once (good for man pages).
> >
> > ~Tim
> >
> > __________ ________________
> > _______/Tim Haynes\__________________________________/Top...@ed.ac.uk\_______
> |> |GCS d? p c++++ l= e* m* s--/+ !n h+ f+ !g w+ t--- r- y? |> | Just
> beneficial than going fishing. |> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
> >
>
> I usually use: `sed -e '/^$/d' mess` to remove blank lines.
> To remove "logically blank" lines, I might use:i
> `sed -e '/^[ \t]*$/d' mess` if sed had a symbolic escape character
> mechanism. Just replace "\t" by a tab character " "
> --
> Walter Briscoe


I would try using grep to pull all lines containing numbers OR alpha
chars thus leaving out blank lines, try this...

grep -E "[a-z]|[A-Z]|[0-9]" $filename

This uses a regular expression to say grep for all alpha chars in the
range of a thru z or zero thru 9; You could also try

grep -i -E "[a-z]|[0-9]" $filename

which should do the same (-i to ignore case, the square brackets denote
a range, and the pipe acts as OR).

Cheers
Randelld
--
## Note the above words of wisdom are mine, ##
## and not always those of my employer. ##
## Reply: rand...@fipltd.demon.co.uk ##


rcha...@rpspo2.atlantaga.ncr.com

unread,
Dec 20, 1996, 3:00:00 AM12/20/96
to

Randell D. wrote:
>
> I would try using grep to pull all lines containing numbers OR alpha
> chars thus leaving out blank lines, try this...
>
> grep -E "[a-z]|[A-Z]|[0-9]" $filename
>
> This uses a regular expression to say grep for all alpha chars in the
> range of a thru z or zero thru 9; You could also try
>
> grep -i -E "[a-z]|[0-9]" $filename
>
> which should do the same (-i to ignore case, the square brackets denote
> a range, and the pipe acts as OR).
>
> Cheers
> Randelld
> --Why not sed '/^[ ]*$/d', takes care of every semblance of
blank line.

--
--------------------------------------------------------
You might be a redneck if you think I speak for NCR
--------------------------------------------------------

Cal Dunigan

unread,
Dec 21, 1996, 3:00:00 AM12/21/96
to

On Fri, 20 Dec 1996 rcha...@rpspo2.atlantaga.ncr.com wrote:

> Randell D. wrote:
> >
> > I would try using grep to pull all lines containing numbers OR alpha
> > chars thus leaving out blank lines, try this...
> >
> > grep -E "[a-z]|[A-Z]|[0-9]" $filename
> >
> > This uses a regular expression to say grep for all alpha chars in the
> > range of a thru z or zero thru 9; You could also try
> >
> > grep -i -E "[a-z]|[0-9]" $filename
> >
> > which should do the same (-i to ignore case, the square brackets denote
> > a range, and the pipe acts as OR).
> >
> > Cheers
> > Randelld
> > --Why not sed '/^[ ]*$/d', takes care of every semblance of
> blank line.

Well, yeah, unless there are non-printable characters.
grep "[!-~]" file

Harrison Bergeron

unread,
Dec 26, 1996, 3:00:00 AM12/26/96
to

Cal Dunigan c...@goodnet.com stated:

>On Fri, 20 Dec 1996 rcha...@rpspo2.atlantaga.ncr.com wrote:
>
>> Randell D. wrote:
>> >
>> > I would try using grep to pull all lines containing numbers OR alpha
>> > chars thus leaving out blank lines, try this...
>> >
>> > grep -E "[a-z]|[A-Z]|[0-9]" $filename
>
>Well, yeah, unless there are non-printable characters.
> grep "[!-~]" file

How about

grep . file

or

tr -s '\n' '\n' <file

--
dav...@davids.psyberlink.net |
Steinberger: | Bah Humbug
State of the Instrument |


0 new messages