Well, I now have a dilemma. I've got a 31,000 line file with some
extra blank lines in it -- I need to get the multiple blank lines out.
That is, I may have a file with five blank lines in a row, and I only
want one blank line there... How do I search for a blank line?
If it were a $ -- which unless my syntax is wrong, it isn't -- could I
do something like:
:1,$s/$*/$/
???
Thanks
--
Andrew Joseph Esposito
mailto:a...@vnet.net
http://users.vnet.net/aje
--
Andrew Joseph Esposito
mailto:/*remove*/aje@/*nospam*/vnet.net
http://users.vnet.net/aje
> Well, I now have a dilemma. I've got a 31,000 line file with some
> extra blank lines in it -- I need to get the multiple blank lines out.
> That is, I may have a file with five blank lines in a row, and I only
> want one blank line there... How do I search for a blank line?
A neat command is
:g/^$/,/./-j
Which is
:g Globally search for pattern
/^$/ Empty line pattern
, Range delimiter, first line is the matched, empty, line
/./ Next line that has a character in it
- Offset, stop at line before the line with a char
j Join the lines in the range (all empty)
Peppe
--
Preben Guldberg __/-\__ "I know that there are people who
c92...@student.dtu.dk (o o) do not love their fellow man, and
----------------------oOOo (_) oOOo I hate people like that!"
http://www.student.dtu.dk/~c928400/ --Tom Lehrer
it's good, but you gotta care for trailing empty lines extra
(these don't have a nonempty successor-line)
> Thus wrote Nonya Business <no...@bogus.net>
> > I use vi & vim for searching and replacing text to create scripts and
> > such.
>
> > Well, I now have a dilemma. I've got a 31,000 line file with some
> > extra blank lines in it -- I need to get the multiple blank lines out.
> > That is, I may have a file with five blank lines in a row, and I only
> > want one blank line there... How do I search for a blank line?
>
> A neat command is
>
> :g/^$/,/./-j
>
> Which is
>
> :g Globally search for pattern
> /^$/ Empty line pattern
> , Range delimiter, first line is the matched, empty, line
> /./ Next line that has a character in it
> - Offset, stop at line before the line with a char
> j Join the lines in the range (all empty)
>
> Peppe
For some reason this didn't work for me (vim 5.3 on Solaris, and I also
tried straight vi). However the following worked like a charm, including
removing the empty lines at the end of the file:
:g/^$/d
--
Walter Prager, Software Designer | ||| "I don't have all the answers. In
TEL: (613) 599-3600 ext. 6460 | |||||||| life, to be honest, I've failed as
EMAIL: wpr...@Newbridge.com | ||||\||| much as I've succeeded. But I love
| ||||\\|| my wife, I love my life, and I wish
NEWBRIDGE NETWORKS CORP | ||||\\\| you *my* kind of success."
Kanata, Ontario, Canada | ||| Dicky Fox
Peppe answered:
> > :g/^$/,/./-j
wpr...@newbridge.com (Walter Prager):
> For some reason this didn't work for me (vim 5.3 on Solaris,
> and I also tried straight vi). However the following worked like
> a charm, including removing the empty lines at the end of the file:
> :g/^$/d
This command deletes *all* empty lines; Nonya does not want this.
The questions was how to "squeeze" multiple empty lines to *one*.
And Peppes command does work - even with vim-5.3 and plain Vi.
I suppose your "empty lines" are not empty. ;-)
Maybe the lines in the file you tried are merely "blank",
ie they appear empty but contain "whitespace" (spaces and tabs).
To "squeeze" blocks of "blank lines" use this command:
:g/^[ ^I]*$/,/[^ ^I]/-j
I prefer Vim's notation "\s" for "space or tab", though,
as it makes commands more readable:
:g/^\s*$/,/^\s/-j
Does this work on your file now?
Anyway, I suggest you let Vim colorize the trailing spaces for you:
" TrailWhitespace: WhiteSpace at end of lines
syn match TrailWhitespace "\s\+$" contains=Spaces
hi TrailWhitespace term=reverse cterm=bold
ctermfg=Black ctermbg=DarkRed guifg=Black guibg=DarkRed
This definition and for some other things is included in
http://www.math.fu-berlin.de/~guckes/vim/syntax/sven.vim
Try it!
Sven
--
Sven Guckes guc...@vim.org - maintainer of | Vim = Vi IMproved | Latest versions
VIM FAQ http://www.vim.org/faq/ | users: VIM-5.3 [980831]
VIM User Page http://www.vim.org/user.html | developers: developing..
VIM Wishlist http://www.vim.org/wish.html | Additions&Corrections welcome! :-)
> > Thus wrote Nonya Business <no...@bogus.net>
> > > I use vi & vim for searching and replacing text to create scripts and
> > > such.
> > > Well, I now have a dilemma. I've got a 31,000 line file with some
> > > extra blank lines in it -- I need to get the multiple blank lines out.
> > > That is, I may have a file with five blank lines in a row, and I only
> > > want one blank line there... How do I search for a blank line?
> > A neat command is
> > :g/^$/,/./-j
[--- snip ---]
> For some reason this didn't work for me (vim 5.3 on Solaris, and I also
> tried straight vi).
Strange, works fine for me (Vim and three vi's: HP-UX, AIX and IRIX.)
There is of course the problem with trailing empy lines.
I have seen people use
:g/^$/.,/./-j
Note the extra dot that specifically sets the start of the range for
each occurance.
> However the following worked like a charm, including
> removing the empty lines at the end of the file:
> :g/^$/d
It should, unfortunately all empty lines between paragraphs are lost,
not just joined.
Peppe
--
Preben Guldberg __/-\_
c92...@student.dtu.dk (o o) If you were a child, it was paradise.
----------------------oOOo (_) oOOo The local adults called it The Pit.
http://www.student.dtu.dk/~c928400/ --Pratchet & Gaiman, _Good Omens_
Bad advice!!, that would also eliminate repeated *NON BLANK* lines !!!
Juan Carlos---
ga...@vmunix.com (Gabor):
> cat file | uniq > new_file
This fails for files which have non-empty repetitive lines:
in:
foo
bar
bar
out:
foo
bar
Btw: "uniq < in > out" suffices. ;-) [*]
Sven
this assumes all non blank lines are already unique. Maybe you can't rely
on this... you could squeeze out valuable information...
Well, I have to correct this. The correct command for Vim is:
:g/^\s*$/,/[^ <c-i>]/-j
The problem is that you cannot use the special notation within sets. :-(
[But I really really hope this will be possible soon. Vim6, Bram?]
So we have to give the literal "counter set" like we have to give for Vi:
[^ <c-i>]
This means, "all characters *not* mathing either a space or a tab".
NOTE: This "global" command uses a "search" and thus affects the last
"search string" to "search for non-blank lines". With "hls" set all
matching non-blank lines will then be highlighted on the current
window which looks as if Vim had gobe into "visual mode" with all
those lines. So you had better turn off "highlight search" with
":set nohls". (Hi, Walter! :-)
> Well, I have to correct this. The correct command for Vim is:
> :g/^\s*$/,/[^ <c-i>]/-j
The previous command actually works just fine: Search until the next
line starting with a whitespace and join until that line. At least
that is what the pattern is :-)
Now if you want to search for the next line with a non-whitespace
character, the alternative to your latest command is
:g/^\s*$/,/\S/-j
> The problem is that you cannot use the special notation within sets. :-(
> [But I really really hope this will be possible soon. Vim6, Bram?]
> So we have to give the literal "counter set" like we have to give for Vi:
> [^ <c-i>]
Actually a you can use "\t" for tabs in ranges, eg "[^ \t]" (along
with \e, \r and \b for <ESC>, <CR> and <BS> respectively (but only
these).
Another possibility is the character classes, eg. [:digit:],
[:alnum:], [:blank:] and [:space:] (the last two are actually
identical). Used in a popular command :-), it could be
:g/^[[:space:]]*$/,/[^[:space]]/-j
Peppe
--
People ask me how I do it, __/-\__ Preben Guldberg
And I say "There's nothin' to it, (o o) c92...@student.dtu.dk
You just stand there lookin' cute, oOOo (_) oOOo---------------------
And when something moves, you shoot!" --Tom Lehrer: The Hunting Song
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SED RESOURCES
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
to master regular expressions
to master sed
join my seders informal email list :
af...@torfree.net
----
sed web pages
"sed one liners" by Eric Pement
"Doing It with sed" by sed stud Carlos J. G. Duarte
seders.icheme.org/ seders grab bag (seders official web page)
www.dbnet.ece.ntua.gr/~george/#seders seder, engineer, Dr2b Yiorgos
www.tec.ualberta.ca/Documentation/Info/by-node/sed-3.02/
ftp://algos.inesc.pt/pub/users/cdua/libs.tar.gz Carlos J. G. Duarte
ftp://algos.inesc.pt/pub/users/cdua/scripts.tar.g Carlos J. G. Duarte
THE SED FAQ
Eric Pement
latest version of the sed FAQ is usually at:
seders.icheme.org/tutorials/sed_faq.txt
www.dbnet.ece.ntua.gr/~george/sed/sedfaq.html
www.ptug.org/sed/sedfaq.html
www.cornerstonemag.com/sed/sedfaq.html
----
www.math.fu-berlin.de/~guckes/sed/ seder Herr Guckes
www.math.fu-berlin.de/~leitner/sed/ Herr Leitner
www.ptug.org/sed/custom_sed.html supersed writer, seder Simon Taylor
www.cis.nctu.edu.tw/~gis84806/sed exotic scripts.seder Yao-Jen Chang
sed/regular expressions tutorials/refs
1- www.dordt.edu:457/OSUserG/BOOKCHAPTER-14.html
Chapter 14, Manipulating text with sed
2- "Doing It with sed" by sed stud Carlos (see http's above)
3- SunOS Manual Pages
http://www.intac.com/man/sunos/sed.1v.html
http://cmgm.stanford.edu/man2html/sed.1v.html
4- u-sedit2. has nice sed docs.
www.cornerstonemag.com/sed/index.htm (new version)
wuarchive.wustl.edu/systems/ibmpc/garbo.uwasa.fi/editor/u-sedit2.zip
5- dc UNIX stack-calculator, in sed, by sed stud GREG UBBEN
www.dbnet.ece.ntua.gr/~george/sed/dc.sed.html
6- sierpinski gasket/triangle, in sed, by sed stud KEN PIZZINI
posted by Al Aab, in July 1998 to
alt.comp.editors.batch & comp.unix.shell
search dejanews
----
some sed implementaions
GNU sed-3.02
for UNIX :
ftp://ftp.gnu.org/pub/gnu/sed-3.02.tar.gz
for DOS :
ftp.urz.uni-heidelberg.de/ftp/pub/simtelnet/gnu/djgpp/v2gnu/sed3
sunsite.doc.ic.ac.uk/packages/simtelnet/gnu/djgpp/v2gnu/sed302b.zip
and many many djgpp mirrors, even in russia & latvia
sedmod.zip extended/awkish DOS sed (bugs)
ftp://ftp.adam.anet.cz/pub/cdrom3/fileutil/sedmod.zip
www.ptug.org/sed/SEDMOD10.ZIP
MKS Toolkit, windows 32 http://www.mks.com
ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/win95/util/
ud32_v43.zip B 3381699 980512 UnixDos: Full Unix set: 65 progs +28 new
utils
ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/win3/util/
ud16_v42.zip B 4767550 980313 UnixDos: Full Unix set. 64 progs +28 new
utils
also ud32...
----
my favourite DOS/UNIX sed :
ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/msdos/txtutl/sed15.zip
ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/msdos/txtutl/sed15x.zip
Directory: /pub/systems/pc/simtelnet/msdos/txtutl/
Filename Type Length Date Description
sed15.zip B 62082 910930 Unix-compatible streaming editor v1.5 TC src
sed15x.zip B 20300 910930 Unix-compatible streaming editor v1.5 EXE/docs
sed15.zip has C source, compilable for UNIX.
sed15.exe compiled with mingw32 for 32bit environments at:
http://www.dbnet.ece.ntua.gr/~george/sed/sed15.exe
----
sed/batch/text newsgroup:
alt.comp.editors.batch
if your newsfeed does not carry it, search dejanews.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Gabor (ga...@vmunix.com) wrote:
: In comp.editors, Nonya Business <no...@bogus.net> wrote :
: # I use vi & vim for searching and replacing text to create scripts and
: # such.
: #
: # Well, I now have a dilemma. I've got a 31,000 line file with some
: # extra blank lines in it -- I need to get the multiple blank lines out.
: # That is, I may have a file with five blank lines in a row, and I only
: # want one blank line there... How do I search for a blank line?
: #
: # If it were a $ -- which unless my syntax is wrong, it isn't -- could I
: # do something like:
: #
: # :1,$s/$*/$/
: #
: # ???
: #
: # Thanks
: cat file | uniq > new_file
--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator sed u soon
it is not zat we do not see the s o l u t i o n
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+