[comp.unix.questions] does vi have a search/deletion macro???

morreale@bierstadt.ucar.edu (Peter Morreale) (09/08/89)

 
Hi!
I'm looking for a functionality that perhaps vi doesn't have.

What I want to do is actually two-fold. 

   1) How can I search for a string and have all occurrences show 
      on my screen (aside from doing a :!grep -i <string> <filename> )
      because then I want to....

   2) delete all lines which match the above mentioned search.

Does vi have macro capability (w/o changing the source or writing C
stuff,  that's beyond me)?  Do I need new glasses to read TFM? 

Can I combine some standard vi stuff and map it to a key? (I can do that ;-)  )

Thanx for any and all hep.  ("hep" is southern for "help")
-PWM
-----------------
Peter W. Morreale                   
Nat'l Center for Atmos. Research, Scientific Computing Division       
morreale@ncar.ucar.edu  (303) 497-1293

yuf@mentor.cc.purdue.edu (Kyle Grieser) (09/08/89)

In article <4237@ncar.ucar.edu> morreale@bierstadt.ucar.edu (Peter Morreale) writes:
>   1) How can I search for a string and have all occurrences show 
>   2) delete all lines which match the above mentioned search.
>

I'm not quite sure I know what you mean here, but searches are really easy.
You can just use "/<string>".  Now if you want to delete lines that have
a match, you can do ":%g/foo/d".  This will delete any line with "foo" on
it.

>Does vi have macro capability (w/o changing the source or writing C
>stuff,  that's beyond me)?  Do I need new glasses to read TFM? 
>
>Can I combine some standard vi stuff and map it to a key? (I can do that ;-)  )

Yeah,  I'm not really sure about the man page, but it is in the standard
"heap-o-unix-manuals" that you should be able to find laying around.
Hope this helps.

-----
Kyle Grieser, Purdue University Computing Center
yuf@mentor.cc.purdue.edu, mentor.cc.purdue.edu!yuf

mchinni@pica.army.mil (Michael J. Chinni, SMCAR-CCS-E) (09/08/89)

Peter,

You wrote:
> What I want to do is actually two-fold. 
>   1) How can I search for a string and have all occurrences show 
>      on my screen (aside from doing a :!grep -i <string> <filename> )
>      because then I want to....
>   2) delete all lines which match the above mentioned search.

One way to do this is via ex from inside vi. For instance, if the string is 
"tornado watch" the command to print AND delete all lines with this string is:
:g/tornado watch/dp

This says:
	invoke ex ( : )
	globally ( g )
	search for the expression "tornado watch" ( /tornado watch/ )
	delete all matching lines ( d )
	print all matching lines ( p )

I suggest doing this as two separate commands (i.e. :g/tornado watch/p then 
:g/tornado watch/d ). This will allow you to be sure that your search string
will only match the lines you really want to delete and not others
inadvertently.

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
			    Michael J. Chinni
      Chief Scientist, Simulation Techniques and Workplace Automation Team
	 US Army Armament Research, Development, and Engineering Center
 User to skeleton sitting at cobweb   () Picatinny Arsenal, New Jersey  
    and dust covered workstation      () ARPA: mchinni@pica.army.mil
      "System been down long?"        () UUCP: ...!uunet!pica.army.mil!mchinni
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

lee@iris.ucdavis.EDU (Peng Lee) (09/09/89)

I have a similar question:  How do you delete a empty line using "sed"
or "ed" ?  I have try the 'sed -e "/^$/d" filename ', but it doesn't work.

-Thanx
Peng (lee@iris.ucdavis.edu)

jik@athena.mit.edu (Jonathan I. Kamens) (09/09/89)

In article <5285@ucdavis.ucdavis.edu> lee@iris.ucdavis.EDU (Peng Lee) writes:
>I have a similar question:  How do you delete a empty line using "sed"
>or "ed" ?  I have try the 'sed -e "/^$/d" filename ', but it doesn't work.
>
>-Thanx
>Peng (lee@iris.ucdavis.edu)

  If the lines are really blank, that will work.  However, they will
often contain tabs or spaces in them.  Therefore, what you should use
is "sed -e '/^[ ^I]*$/d' filename" (the ^I is a real tab character).
Watch this:

Script started on Fri Sep  8 14:44:19 1989
vulcan% cat test
This is a test file.
There is a blank line here:

Here's a line with a space on it:
 
And here's one with a tab on it:
        
And here's one with a space and a tab:
        
And this is the end of the file.
vulcan% sed '/^$/d' test
This is a test file.
There is a blank line here:
Here's a line with a space on it:
 
And here's one with a tab on it:
        
And here's one with a space and a tab:
        
And this is the end of the file.
vulcan% sed '/^[ ]*$/d' test
This is a test file.
There is a blank line here:
Here's a line with a space on it:
And here's one with a tab on it:
        
And here's one with a space and a tab:
        
And this is the end of the file.
vulcan% sed '/^[^I]*$/d' test
This is a test file.
There is a blank line here:
Here's a line with a space on it:
 
And here's one with a tab on it:
And here's one with a space and a tab:
        
And this is the end of the file.
vulcan% sed '/^[ ^I]*$d/' test
This is a test file.
There is a blank line here:
Here's a line with a space on it:
And here's one with a tab on it:
And here's one with a space and a tab:
And this is the end of the file.
vulcan% exit
script done on Fri Sep  8 14:45:24 1989

Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-4261			      Home: 617-782-0710

dberg@cod.NOSC.MIL (David I. Berg) (09/10/89)

In article <4237@ncar.ucar.edu>, morreale@bierstadt.ucar.edu (Peter Morreale) writes:
>    1) How can I search for a string and have all occurrences show 
>       on my screen (aside from doing a :!grep -i <string> <filename> )
>       because then I want to....
> 
>    2) delete all lines which match the above mentioned search.

To display all lines with a character string satisfying a search criterion,
use      :g/<string>/p      (That says global search for string and print.)
If you want to list the lines with line numbers, use "nu" in place of "p".

To delete all lines with a character string satisfying a search criterion,
use      :g/<string>/d      (That says global search for string and delete.)

-- 
David I. Berg (dberg@nosc.mil)
GENISYS Information Systems, Inc., 4250 Pacific Hwy #118, San Diego, CA 92110
MILNET: dberg@nosc.mil
UUCP:   {akgua decvax dcdwest ucbvax}!sdcsvax!noscvax!dberg

abcscnge@csuna.csun.edu (Scott "The Pseudo-Hacker" Neugroschl) (09/12/89)

In article <5285@ucdavis.ucdavis.edu> lee@iris.ucdavis.EDU (Peng Lee) writes:
>I have a similar question:  How do you delete a empty line using "sed"
>or "ed" ?  I have try the 'sed -e "/^$/d" filename ', but it doesn't work.

You might want to try single quotes in the sed command, viz:

	sed -e '/^$/d' filename

as double quotes will attempt to expand the $.


-- 
Scott "The Pseudo-Hacker" Neugroschl		abcscnge@csuna.csun.edu
-- Beat me, Whip me, make me code in Ada
-- Disclaimers?  We don't need no stinking disclaimers!!!