Match something that not in the pattern

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

Match something that not in the pattern

Peng Yu
Hi,

I have the following file segments. I want to concatenate all the
lines with their next lines, except that it ends with "}}". I want to
use the pattern "\(}}\)\@!\n^". It seems not working.

Would you please help me to figure out how to match the lineend without "}}"?

Thanks,
Peng


   2.3766829304614354*^-12*d^3*Ith^2*z^6 -
   9.163340710959959*^-10*Ith^3*z^6 - 3.0207696015850803*^-10*d*
    Ith^3*z^6 + 8.760261318791164*^-11*d^2*Ith^3*z^6 -
   3.492315374248676*^-12*d^3*Ith^3*z^6}}
fw100s200[d_,z_,Ith_] := 0.01515647826357657, 0.08956492396538135,
  246.40722214248873 - 0.48219974209354677*d +
   0.07494738483672195*d^2 - 0.0007155767500598704*d^3 -
   778.7338684688602*Ith + 5.330667362452238*d*Ith -
   0.6096508973274091*d^2*Ith + 0.00429237191519287*d^3*Ith +
   1465.6757219477772*Ith^2 - 15.92585633547399*d*Ith^2 +
Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Anupam Srivastava-3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peng Yu wrote:

> Hi,
>
> I have the following file segments. I want to concatenate all the
> lines with their next lines, except that it ends with "}}". I want to
> use the pattern "\(}}\)\@!\n^". It seems not working.
>
> Would you please help me to figure out how to match the lineend without
> "}}"?
>
> Thanks,
> Peng
>
>
>   2.3766829304614354*^-12*d^3*Ith^2*z^6 -
>   9.163340710959959*^-10*Ith^3*z^6 - 3.0207696015850803*^-10*d*
>    Ith^3*z^6 + 8.760261318791164*^-11*d^2*Ith^3*z^6 -
>   3.492315374248676*^-12*d^3*Ith^3*z^6}}
> fw100s200[d_,z_,Ith_] := 0.01515647826357657, 0.08956492396538135,
>  246.40722214248873 - 0.48219974209354677*d +
>   0.07494738483672195*d^2 - 0.0007155767500598704*d^3 -
>   778.7338684688602*Ith + 5.330667362452238*d*Ith -
>   0.6096508973274091*d^2*Ith + 0.00429237191519287*d^3*Ith +
>   1465.6757219477772*Ith^2 - 15.92585633547399*d*Ith^2 +
Well, I am still learning VIM but these 3 commands could do your job, if
they are acceptable to you (they work for me!):
:1,10s/\n/ \r/g
:1,10s/}} /}}/g
:1,10s/ \n//g

:)
- --
Anupam Srivastava
Scientific Coworker
Universität Stuttgart

Get my public keys:
gpg --keyserver subkeys.pgp.net --recv-keys DF8B2AFE 99F8BB81
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFFNtE27GZ7yN+LKv4RAgBQAJ9kEt5G99gxO1eZh9Rs+3XeZDVixQCfSj8p
H2FzBiSjkjRSbby//mVRf94=
=eR5i
-----END PGP SIGNATURE-----
Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Bill McCarthy
In reply to this post by Peng Yu
On Wed 18-Oct-06 8:03pm -0600, Peng Yu wrote:

> I have the following file segments. I want to concatenate all the
> lines with their next lines, except that it ends with "}}". I want to
> use the pattern "\(}}\)\@!\n^". It seems not working.
>
> Would you please help me to figure out how to match the lineend without "}}"?

You're fairly close.  Assuming you visually marked those
lines, this solution is magic:

    '<,'>s/\%(}}\)\@<!\n

but this solution is very magic:

    '<,'>s/\v%(}})@<!\n

:h /\@<!
:h /\v

--
Best regards,
Bill

Reply | Threaded
Open this post in threaded view
|

Fwd: Match something that not in the pattern

Peng Yu
---------- Forwarded message ----------
From: Peng Yu <[hidden email]>
Date: Oct 18, 2006 9:19 PM
Subject: Re: Match something that not in the pattern
To: Bill McCarthy <[hidden email]>


On 10/18/06, Bill McCarthy <[hidden email]> wrote:

> On Wed 18-Oct-06 8:03pm -0600, Peng Yu wrote:
>
> > I have the following file segments. I want to concatenate all the
> > lines with their next lines, except that it ends with "}}". I want to
> > use the pattern "\(}}\)\@!\n^". It seems not working.
> >
> > Would you please help me to figure out how to match the lineend without "}}"?
>
> You're fairly close.  Assuming you visually marked those
> lines, this solution is magic:
>
>     '<,'>s/\%(}}\)\@<!\n
>
> but this solution is very magic:
>
>     '<,'>s/\v%(}})@<!\n
>
> :h /\@<!
> :h /\v


Could you please explain what these commands mean?

Thanks,
Peng
Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Peter Hodge-2
In reply to this post by Peng Yu

--- Peng Yu <[hidden email]> wrote:

> Hi,
>
> I have the following file segments. I want to concatenate all the
> lines with their next lines, except that it ends with "}}". I want to
> use the pattern "\(}}\)\@!\n^". It seems not working.

\@! is the look-ahead assersion, you want the look-behind assertion which is
\@<!

So you could use "\(}}\)\@<!\n^" instead.

regards,
peter


               
____________________________________________________
On Yahoo!7
Break a world record with Total Girl's WorldÂ’s Largest Slumber Party
http://www.totalgirl.com.au/slumberparty
Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

A.J.Mechelynck
In reply to this post by Peng Yu
Peng Yu wrote:

> Hi,
>
> I have the following file segments. I want to concatenate all the
> lines with their next lines, except that it ends with "}}". I want to
> use the pattern "\(}}\)\@!\n^". It seems not working.
>
> Would you please help me to figure out how to match the lineend without
> "}}"?
>
> Thanks,
> Peng

To join every line not ending in }} with the following line, use the ":j[oin]"
command on every line _not_ matching the pattern /}}$/ which means "two
closing braces at end of line". As a cherry on the cake, you can avoid joining
the last line in the file (which has no following line):

        :1,$-1v/}}$/j

See
        :help multi-repeat
        :help :join


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Bill McCarthy
In reply to this post by Peng Yu
On Wed 18-Oct-06 9:20pm -0600, Peng Yu wrote:
> On 10/18/06, Bill McCarthy <[hidden email]> wrote:

>> You're fairly close.  Assuming you visually marked those
>> lines, this solution is magic:
>>
>>     '<,'>s/\%(}}\)\@<!\n
>>
>> but this solution is very magic:
>>
>>     '<,'>s/\v%(}})@<!\n
>>
>> :h /\@<!
>> :h /\v

> Could you please explain what these commands mean?

Sure, let's use the first one:

    '<,'>s/\%(}}\)\@<!\n

(1) '<,'>s/pattern

    After you mark the text to be modified, hitting ":"
    gives you, on the command line, ":'<,'>".  So all you
    type is ":s/pattern" - the "'<,'>" is typed by Vim.

    This command deletes the first occurrence of "pattern"
    on each line in the range.

(2) Instead of "pattern" I used pat1\@<!pat2

    That finds a "pat2" that is not followed by a "pat1".

(3) For "pat1" I used '\%(}}\)'

    I don't want EOLs that follow "}}".  However, \@<!
    operates on the atom to its left so it would only
    operate on the second "}" - there for I've grouped it
    with a non-capturing \%(\).

(4) "pat2" is just the EOL which is "\n".

(5) Note that it is "pat2" that is found and replaced, not
    "}}\n".

Here's a much simpler one, but its done in two steps and
would add an EOL to any embedded "}}":

    '<,'>j|s/}}/&\r/g

This is equivalent to type two commands:

(1) '<,'>j

    That joins all the lines in the range to just one line.

(2) s/}}/&\r/g

    That substitute command operates on the single resulting
    line of the join command command.  Every '}}' will be
    replaced by '}}\r'.  Where the '&' is replaced by the
    whole matched pattern and '\r' is an EOL (although '\n'
    is an EOL in a pattern).
   
--
Best regards,
Bill

Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Bill McCarthy
In reply to this post by A.J.Mechelynck
On Wed 18-Oct-06 10:24pm -0600, A.J.Mechelynck wrote:

> Peng Yu wrote:
>> Hi,
>>
>> I have the following file segments. I want to concatenate all the
>> lines with their next lines, except that it ends with "}}". I want to
>> use the pattern "\(}}\)\@!\n^". It seems not working.
>>
>> Would you please help me to figure out how to match the lineend without
>> "}}"?
>>
>> Thanks,
>> Peng
>
> To join every line not ending in }} with the following line, use the ":j[oin]"
> command on every line _not_ matching the pattern /}}$/ which means "two
> closing braces at end of line". As a cherry on the cake, you can avoid joining
> the last line in the file (which has no following line):
>
>         :1,$-1v/}}$/j
>
> See
>         :help multi-repeat
>         :help :join

Let's assume those 10 lines in the example were the only
lines in the buffer.

vglobal will first mark lines 1-3 and 5-9.  It will next
apply the join command to each of those lines.  The line
numbers below refer to these original line numbers.

The join on 1 will join lines 1&2, the no longer existing
line 2 is skipped, join lines 3&4, join lines 5&6, skip 6,
join 7&8, skip 8 and, finally, join 9&10.

You end up with 5 lines.  The goal is to end with 2 lines.

--
Best regards,
Bill

Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

A.J.Mechelynck
Bill McCarthy wrote:

> On Wed 18-Oct-06 10:24pm -0600, A.J.Mechelynck wrote:
>
>> Peng Yu wrote:
>>> Hi,
>>>
>>> I have the following file segments. I want to concatenate all the
>>> lines with their next lines, except that it ends with "}}". I want to
>>> use the pattern "\(}}\)\@!\n^". It seems not working.
>>>
>>> Would you please help me to figure out how to match the lineend without
>>> "}}"?
>>>
>>> Thanks,
>>> Peng
>> To join every line not ending in }} with the following line, use the ":j[oin]"
>> command on every line _not_ matching the pattern /}}$/ which means "two
>> closing braces at end of line". As a cherry on the cake, you can avoid joining
>> the last line in the file (which has no following line):
>>
>>         :1,$-1v/}}$/j
>>
>> See
>>         :help multi-repeat
>>         :help :join
>
> Let's assume those 10 lines in the example were the only
> lines in the buffer.
>
> vglobal will first mark lines 1-3 and 5-9.  It will next
> apply the join command to each of those lines.  The line
> numbers below refer to these original line numbers.
>
> The join on 1 will join lines 1&2, the no longer existing
> line 2 is skipped, join lines 3&4, join lines 5&6, skip 6,
> join 7&8, skip 8 and, finally, join 9&10.
>
> You end up with 5 lines.  The goal is to end with 2 lines.
>

Hm, I see what you mean. Let's try a variation:

        :1,$-1v/}}$/while getline(".") !~ '}}$' | join | endwhile


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Bill McCarthy
On Thu 19-Oct-06 2:28am -0600, you wrote:

> Bill McCarthy wrote:
>> On Wed 18-Oct-06 10:24pm -0600, A.J.Mechelynck wrote:
>>
>>> Peng Yu wrote:
>>>> Hi,
>>>>
>>>> I have the following file segments. I want to concatenate all the
>>>> lines with their next lines, except that it ends with "}}". I want to
>>>> use the pattern "\(}}\)\@!\n^". It seems not working.
>>>>
>>>> Would you please help me to figure out how to match the lineend without
>>>> "}}"?
>>>>
>>>> Thanks,
>>>> Peng
>>> To join every line not ending in }} with the following line, use the ":j[oin]"
>>> command on every line _not_ matching the pattern /}}$/ which means "two
>>> closing braces at end of line". As a cherry on the cake, you can avoid joining
>>> the last line in the file (which has no following line):
>>>
>>>         :1,$-1v/}}$/j
>>>
>>> See
>>>         :help multi-repeat
>>>         :help :join
>>
>> Let's assume those 10 lines in the example were the only
>> lines in the buffer.
>>
>> vglobal will first mark lines 1-3 and 5-9.  It will next
>> apply the join command to each of those lines.  The line
>> numbers below refer to these original line numbers.
>>
>> The join on 1 will join lines 1&2, the no longer existing
>> line 2 is skipped, join lines 3&4, join lines 5&6, skip 6,
>> join 7&8, skip 8 and, finally, join 9&10.
>>
>> You end up with 5 lines.  The goal is to end with 2 lines.
>>

> Hm, I see what you mean. Let's try a variation:
>
>         :1,$-1v/}}$/while getline(".") !~ '}}$' | join | endwhile

That could result in an infinite loop if there were no '}}$'
in either of the last 2 lines - and an incorrect result if
there was no '}}$' in the last line.

It would work by replacing:

    getline(".") !~ '}}$'

with:

    getline(".") !~ '}}$' && line(".") != line("$")

--
Best regards,
Bill

Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

A.J.Mechelynck
Bill McCarthy wrote:

> On Thu 19-Oct-06 2:28am -0600, you wrote:
>
>> Bill McCarthy wrote:
>>> On Wed 18-Oct-06 10:24pm -0600, A.J.Mechelynck wrote:
>>>
>>>> Peng Yu wrote:
>>>>> Hi,
>>>>>
>>>>> I have the following file segments. I want to concatenate all the
>>>>> lines with their next lines, except that it ends with "}}". I want to
>>>>> use the pattern "\(}}\)\@!\n^". It seems not working.
>>>>>
>>>>> Would you please help me to figure out how to match the lineend without
>>>>> "}}"?
>>>>>
>>>>> Thanks,
>>>>> Peng
>>>> To join every line not ending in }} with the following line, use the ":j[oin]"
>>>> command on every line _not_ matching the pattern /}}$/ which means "two
>>>> closing braces at end of line". As a cherry on the cake, you can avoid joining
>>>> the last line in the file (which has no following line):
>>>>
>>>>         :1,$-1v/}}$/j
>>>>
>>>> See
>>>>         :help multi-repeat
>>>>         :help :join
>>> Let's assume those 10 lines in the example were the only
>>> lines in the buffer.
>>>
>>> vglobal will first mark lines 1-3 and 5-9.  It will next
>>> apply the join command to each of those lines.  The line
>>> numbers below refer to these original line numbers.
>>>
>>> The join on 1 will join lines 1&2, the no longer existing
>>> line 2 is skipped, join lines 3&4, join lines 5&6, skip 6,
>>> join 7&8, skip 8 and, finally, join 9&10.
>>>
>>> You end up with 5 lines.  The goal is to end with 2 lines.
>>>
>
>> Hm, I see what you mean. Let's try a variation:
>>
>>         :1,$-1v/}}$/while getline(".") !~ '}}$' | join | endwhile
>
> That could result in an infinite loop if there were no '}}$'
> in either of the last 2 lines - and an incorrect result if
> there was no '}}$' in the last line.
>
> It would work by replacing:
>
>     getline(".") !~ '}}$'
>
> with:
>
>     getline(".") !~ '}}$' && line(".") != line("$")
>

Hadn't thought of that. So IIUC our final try is

        1,$-1v/}}$/
                \ while getline(".") !~ '}}$'
                \ && line(".") != line ("$")
                \ |  join
                \ | endwhile

isn't it?


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Bill McCarthy
On Thu 19-Oct-06 3:08am -0600, you wrote:

> Hadn't thought of that. So IIUC our final try is

>         1,$-1v/}}$/
>                 \       while getline(".") !~ '}}$'
>                 \               && line(".") != line ("$")
>                 \ |       join
>                 \ |     endwhile

> isn't it?

For this vglobal approach, yes.

I still prefer:

    %s/\v%(}})@<!\n

--
Best regards,
Bill

Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Charles E Campbell Jr
In reply to this post by Peng Yu
Peng Yu wrote:

> Hi,
>
> I have the following file segments. I want to concatenate all the
> lines with their next lines, except that it ends with "}}". I want to
> use the pattern "\(}}\)\@!\n^". It seems not working.
>
> Would you please help me to figure out how to match the lineend
> without "}}"?

This problem is one that LogiPat can help with!

  (see http://vim.sourceforge.net/scripts/script.php?script_id=1290
   or http://mysite.verizon.net/astronaut/vim/index.html#VimFuncs, see
"LogiPat")

First, let's construct a pattern that matches all lines that don't end
with }}:

:echo LogiPat('!"}}$"')

^\%(\%(}}$\)\@!.\)*$

Now, that has a bit more than is necessary, but it does work.  I'm not
sure what
you want with that trailing '^' -- was that supposed to mean
beginning-of-line?
It isn't necessary if that's the case.

So, to do what you ask with LogiPat's result:

:g/^\%(\%(}}$\)\@!.\)*$/j

Regards,
Chip Campbell



Reply | Threaded
Open this post in threaded view
|

Re: Match something that not in the pattern

Bill McCarthy
On Thu 19-Oct-06 9:26am -0600, Charles E Campbell Jr wrote:

> So, to do what you ask with LogiPat's result:
>
> :g/^\%(\%(}}$\)\@!.\)*$/j

Please see my response to Tony Mechelynck.  Your solution
changes the text of Peng Yu's example to 5 lines.  As I
understand his goal, the correct answer has 2 lines.

It is not a problem with your very clever LogiPat, it is the
application within :global.

Here's what I believe is happening during the original first
4 lines:

First you join the first two lines.  Then, since the
original line 2 is no longer present, nothing is done with
line 2.  Next it joins lines 3 and 4.

That's 2 lines so far.  Actually, lines 1 through 4 should
be joined to 1 line.

--
Best regards,
Bill