LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-28-2010, 11:38 PM   #1
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908
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, 12:09 AM   #2
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: RHEL/Ubuntu/Debian/Fedora/Centos/K3OS
Posts: 1,159
Blog Entries: 4

Rep: Reputation: 149Reputation: 149
:%s/✔//gn
 
Old 09-29-2010, 12: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, 12: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, 12:32 AM   #5
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

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, 12:40 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 01:25 AM   #7
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

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 01:27 AM.
 
Old 09-29-2010, 01:49 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 01:54 AM   #9
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

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, 05:03 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 01:40 AM   #11
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

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, 02:01 AM   #12
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

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



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 07:36 AM
Capturing Output from net ads dns register Command jfmorales Linux - General 2 04-07-2010 03:07 PM
Capturing output from background FTP command Lazypete Linux - Server 6 05-22-2008 10:41 AM
Perl: Running Command line apps in background and capturing output s0l1dsnak3123 Programming 8 03-28-2008 01:24 PM
expect_out(buffer) is not capturing all output from a Cisco IOS command eentonig Programming 1 01-30-2008 07:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:10 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
Open Source Consulting | Domain Registration