delete only the short sentences

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

delete only the short sentences

Antonio Recio
I want to delete all the lines with long < 40 characters (including
white-spaces) and keep the others. Is there any command to do that?

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Antonio Recio
And to keep only the sentences in the range 40-60 characters long and
delete the rest?

On Nov 1, 4:32 am, AMDx64BT <[hidden email]> wrote:
> I want to delete all the lines with long < 40 characters (including
> white-spaces) and keep the others. Is there any command to do that?

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Joan Miquel Torres Rigo
2010/11/1 AMDx64BT <[hidden email]>:
> And to keep only the sentences in the range 40-60 characters long and
> delete the rest?

:g/^.\{,39\}$/d

and

:g/\(^.\{,39\}$\)\|\(^.\{61,\}$\)/d

should work.

>
> On Nov 1, 4:32 am, AMDx64BT <[hidden email]> wrote:
>> I want to delete all the lines with long < 40 characters (including
>> white-spaces) and keep the others. Is there any command to do that?
>
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>



--
Joan Miquel Torres__________________________________
Linux Registered User #164872
http://www.mallorcaweb.net/joanmiquel
BULMA: http://bulma.net http://breu.bulma.net/?l2301

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Tim Chase
On 10/31/10 23:14, Joan Miquel Torres Rigo wrote:
> 2010/11/1 AMDx64BT<[hidden email]>:
>> And to keep only the sentences in the range 40-60 characters long and
>> delete the rest?
>
> :g/^.\{,39\}$/d
>
> and
>
> :g/\(^.\{,39\}$\)\|\(^.\{61,\}$\)/d

And to reverse the condition, one could use something like

   :v/\%40c./d

("if there's a character at position 40, don't delete this row;
otherwise, delete it")

-tim



--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Tim Chase
On 11/01/10 07:40, Joan Miquel Torres Rigo wrote:
 > 2010/11/1 Tim Chase<[hidden email]>:

>>   :v/\%40c./d
>>
>> ("if there's a character at position 40, don't delete this
>> row; otherwise, delete it")
>
> Then you will also remove rows with 61 to 80 characters.
>
> But the strategy is probably good. (I'm not fully expert with
> vim's regular expressions yet and I don't know the meaning of
> '%' in this context).

Actually, the problem is the reverse -- it fails to delete lines
 > 80 characters.  The \%40c is an atom that matches at column 40

   :help /\%c

so any line that doesn't have a character at position 40 (lines
<40 chars) will be deleted.  (note that I don't anchor the
column-position to the end-of-line)  I'd usually[1] do this as a
2-pass, taking out those >80 in the 2nd pass:

   :v/\%40c./d  " delete anything shorter than 40
   :g/\%81c./d  " delete anything longer than 80

I might have some fence-posting errors in there, so 40 might need
to be 39 or 41 and 81 might also be +/- 1, but the logic still holds.

-tim


[1]
Yes, I've had to do the same thing as the OP on several occasions
with various shorter/longer-than thresholds; usually in
column-delimited files with garbage before/after the actual data.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Nikolay Aleksandrovich Pavlov
Reply to message «Re: delete only the short sentences»,
sent 17:21:40 01 November 2010, Monday
by Tim Chase:

> On 11/01/10 07:40, Joan Miquel Torres Rigo wrote:
>  > 2010/11/1 Tim Chase<[hidden email]>:
> >>   :v/\%40c./d
> >>
> >> ("if there's a character at position 40, don't delete this
> >> row; otherwise, delete it")
> >
> > Then you will also remove rows with 61 to 80 characters.
> >
> > But the strategy is probably good. (I'm not fully expert with
> > vim's regular expressions yet and I don't know the meaning of
> > '%' in this context).
>
> Actually, the problem is the reverse -- it fails to delete lines
>
>  > 80 characters.  The \%40c is an atom that matches at column 40
>  >
>    :help /\%c
>
> so any line that doesn't have a character at position 40 (lines
> <40 chars) will be deleted.  (note that I don't anchor the
> column-position to the end-of-line)  I'd usually[1] do this as a
>
> 2-pass, taking out those >80 in the 2nd pass:
>    :v/\%40c./d  " delete anything shorter than 40
>    :g/\%81c./d  " delete anything longer than 80
>
> I might have some fence-posting errors in there, so 40 might need
> to be 39 or 41 and 81 might also be +/- 1, but the logic still holds.
>
> -tim
>
>
> [1]
> Yes, I've had to do the same thing as the OP on several occasions
> with various shorter/longer-than thresholds; usually in
> column-delimited files with garbage before/after the actual data.
s/character/byte/
\%c does nothing with characters, it operates on byte columns. You should use
\%v for displayed columns (unless you have a tab or non-printable character
somewhere before Nth character, \%Nv will match at Nth character).

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Antonio Recio
In reply to this post by Tim Chase
Thanks for the solution. It works. I would like to know how I can
delete sentences < 40 characters only when they are more than 3 times
consecutives.

For example, with this text:

xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx

Obtain this:

xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Thanks in advance.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Christian Brabandt
Hi AMDx64BT!

On So, 07 Nov 2010, AMDx64BT wrote:

> Thanks for the solution. It works. I would like to know how I can
> delete sentences < 40 characters only when they are more than 3 times
> consecutives.
>
> For example, with this text:
>
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
>
> Obtain this:
>
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> Thanks in advance.

try this:
:s/\_^\(.\{,40}\n\)\{4,}//

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

rameo
In reply to this post by Antonio Recio
or this one:

:v/^.\{41,}/d

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Antonio Recio
In reply to this post by Christian Brabandt
:%s/\_^\(.\{,40}\n\)\{4,}// works very well. I would like to do
another question.
Could be possible to delete all the sentences < 40 characters only
when they are more than 3 times consecutives "but now keeping the last
one undeleted of each group". I want to say this:

Begining with this:

xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xx--Title--xx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

And obtain this:

xx--Title--xx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: delete only the short sentences

Christian Brabandt
Hi AMDx64BT!

On Mo, 08 Nov 2010, AMDx64BT wrote:

> :%s/\_^\(.\{,40}\n\)\{4,}// works very well. I would like to do
> another question.
> Could be possible to delete all the sentences < 40 characters only
> when they are more than 3 times consecutives "but now keeping the last
> one undeleted of each group". I want to say this:
>
> Begining with this:
>
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
> xxxxxxxxxx
> xx--Title--xx
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> And obtain this:
>
> xx--Title--xx
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

:%s/\_^\%(.\{,40}\n\)\{3,}\(.\{,40}$\)/\1/

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php