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 ? |
:%s/✔//gn
|
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, |
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 |
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. |
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:
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 |
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 |
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. |
I also did
:silent command This reduced content in register to ^J^J2 matches on 1 line^J So no repeatition of output message. |
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
|
yeah , I use them to do repetitive tasks. Though I am not familiar with using vim functions which is slightly complicated.
|
Thanks grail.
The awk statement worked as desired. In the process , I read man page of awk that taught Quote:
|
All times are GMT -5. The time now is 06:19 PM. |