LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   capturing output of command in vim editor (https://www.linuxquestions.org/questions/linux-newbie-8/capturing-output-of-command-in-vim-editor-835082/)

sumeet inani 09-28-2010 11:38 PM

capturing output of command in vim editor
 
I have a file which contains a table

row1 ✔ no ✔ no
row2 no ✔ ✔ no
... so on


I want to prefix the number of ✔ to corresponding row
If I use the command
:.s/✔//gn
I get output written like '2 matches on 1 line'

How can I extract the '2 matches' in above case ?

prayag_pjs 09-29-2010 12:09 AM

:%s/✔//gn

greplinux 09-29-2010 12:13 AM

Hi,

I didn't understand your question fully. But I can give you a method through which you can move the search results to a buffer for later manipulation.

1. :let @a="" ( Clear the register 'a')
2. :%g/<match string>/y A ( Move the match strings to register 'a' in append mode)
3. "ap ( Open a new file and run this to paste the contents copied)

Hope it helps.

Thanks,

btncix 09-29-2010 12:30 AM

This doesn't answer your questions, and you probably know this already, but just in case you don't and also to help other people understand your question - I recommend you used 1)sed & awk or 2)perl to extract the information.

So the output you want would be
2 matches on line 1
2 matches on line 2
etc.

I would love to know if vi can do this for you instead of having to use sed/awk or perl

sumeet inani 09-29-2010 12:32 AM

to prayag_pjs
What you said just meant - tell the number of occurences of ✔ in whole file.

to greplinux
I tried yours but did it did not copy anything like 'x matches in y lines' into register a.

grail 09-29-2010 12:40 AM

Well I also am not a 100% sure of the question, but if I understand correctly, based on your current example:

row1 ✔ no ✔ no

This would end up looking like:

2 row1 ✔ no ✔ no

I would think that it is probably possible, but I very much doubt it is simple or even along the lines you are looking at.
Also, even if it were possible for the single line, you would run into larger issues over multiple lines. If you did use prayag_pjs's
example you would find based on your example that the answer would be:
Quote:

4 matches on 2 lines
Which of course would not help :(

As opposed to btncix I think sed will be almost as much grief (may just be my lack of knowledge) as vi(m) is using this type of functionality so same problem as above.

My choice would be awk or above, like Perl:
Code:

awk -F"✔" '{print (NF-1),$0}' file > new_file
btw. only using your tick as an example as of course awk would not really recognise this symbol.

sumeet inani 09-29-2010 01:25 AM

I found a little solution
:let @a=""
:redir @a
:Your ex command
:redir END
Now the output eg.:-'10 matches on 1 line' appeared in register a (though repeated twice) .

I knew vim editor can do such little things.

I will read :help redir

grail 09-29-2010 01:49 AM

Actually I found when I pasted the register into my document it was actually 4 lines, the first 2 being blank.

As I said before though, you will still face the issue of how to process for each line. You will more than likely need to create your own function so you can loop through
the data.

sumeet inani 09-29-2010 01:54 AM

I also did
:silent command
This reduced content in register to
^J^J2 matches on 1 line^J
So no repeatition of output message.

chrism01 09-29-2010 05:03 AM

If you need to repeat a set of cmds you could look at vim macros http://vimdoc.sourceforge.net/htmldoc/usr_10.html#10.1

sumeet inani 09-30-2010 01:40 AM

yeah , I use them to do repetitive tasks. Though I am not familiar with using vim functions which is slightly complicated.

sumeet inani 03-22-2011 02:01 AM

Thanks grail.
The awk statement worked as desired.
In the process , I read man page of awk that taught
Quote:

awk [options] file
NF --total number of fields
-F symbol --field separator
$0 --whole record


All times are GMT -5. The time now is 06:19 PM.