Saturday, September 04, 2021

Re: vi: count occurrences of a substring

O 04/09/21 ás 18:25, ropers escribiu:
> On 04/09/2021, Parodper <parodper@gmail.com> wrote:
>> So I wrote
>> :!sed s/abc/abc\/g % | grep -c abc
>> and then went back and pressed <ENTER> after that backslash, i.e.
>> :!sed s/abc/abc\<ENTER>/g % | grep -c abc
>> And it gave me a correct number of abc's for my test text.
>
> I feel like the dumbest person in the world asking this, but what
> EXACTLY do you mean by "and then went back"?
> Are you using cursor keys? I.e. should I have gotten those to work in
> vi in xterm and console? Because I haven't. The moment I try to
> cursor back, I'm back to vi mode and the ex-style command mode line at
> the bottom is gone.

Yeah, moving the cursor back and pressing enter. On the vi subject,
since I don't have my OpenBSD machine at hand I was testing this on
Debian GNU/Linux with sed --posix, and it has vim instead of vi.

> Otherwise, if I try to just type
> :!sed s/abc/abc\/g % | grep -c abc
> and press enter, I only get the same output I also get out of
> :!grep -c abc %
> on its own -- which won't count multiple same-line occurrences.
>
> A still confused
> Ian
>

Now it is my turn to feel dumb. I was so focused on the newline-sed
subject that I forgot about that, so I did not put multiple abc's on the
same line :).

On that subject
:! grep -o abc % | wc -l
seems to work for me.

No comments:

Post a Comment