LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-29-2010, 12:38 AM   #1
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Rep: Reputation: 49
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 ?
 
Old 09-29-2010, 01:09 AM   #2
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: Fedora,RedHat,CentOS,Gentoo
Posts: 1,138
Blog Entries: 4

Rep: Reputation: 147Reputation: 147
:%s/✔//gn
 
Old 09-29-2010, 01:13 AM   #3
greplinux
Member
 
Registered: Jun 2007
Posts: 118

Rep: Reputation: 17
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,
 
Old 09-29-2010, 01:30 AM   #4
btncix
Member
 
Registered: Aug 2009
Location: USA
Posts: 141

Rep: Reputation: 26
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
 
Old 09-29-2010, 01:32 AM   #5
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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.
 
Old 09-29-2010, 01:40 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
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.
 
1 members found this post helpful.
Old 09-29-2010, 02:25 AM   #7
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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

Last edited by sumeet inani; 09-29-2010 at 02:27 AM.
 
Old 09-29-2010, 02:49 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
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.
 
Old 09-29-2010, 02:54 AM   #9
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
I also did
:silent command
This reduced content in register to
^J^J2 matches on 1 line^J
So no repeatition of output message.
 
Old 09-29-2010, 06:03 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
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
 
Old 09-30-2010, 02:40 AM   #11
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
yeah , I use them to do repetitive tasks. Though I am not familiar with using vim functions which is slightly complicated.
 
Old 03-22-2011, 03:01 AM   #12
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command vi -w /etc/aliases is not opening in VIM editor kvijaik Linux - Newbie 6 02-02-2012 08:36 AM
Capturing Output from net ads dns register Command jfmorales Linux - General 2 04-07-2010 04:07 PM
Capturing output from background FTP command Lazypete Linux - Server 6 05-22-2008 11:41 AM
Perl: Running Command line apps in background and capturing output s0l1dsnak3123 Programming 8 03-28-2008 02:24 PM
expect_out(buffer) is not capturing all output from a Cisco IOS command eentonig Programming 1 01-30-2008 08:29 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration