LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-23-2007, 03:07 PM   #1
zim64half
LQ Newbie
 
Registered: Apr 2007
Location: Chicago, IL
Posts: 1

Rep: Reputation: 0
Question how do I delete *certain* lines in vi editor?


Howdy.

This is my first post here. Looks like a very active forum!

I am looking for a way to search a file for a specific string, and find all of the lines that contain that string.

Then - delete all of those lines without changing any of the other lines.

Is this possible in vi?

This is an especially large file, so I want it to happen all at once instead of having to confirm each deleted line.

Thanks!

-Zim
 
Old 04-23-2007, 03:17 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Why not use sed? For example, to delete all the line containing the string B. Gates
Code:
sed -i "/B. Gates/d" filename
Of course, make a copy of the file first!!

[EDIT:] Ooops! Revised the command so it does in-place editting.

Last edited by weibullguy; 04-24-2007 at 07:58 AM.
 
Old 04-23-2007, 06:54 PM   #3
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
If you prefer a more "manual" way using vi, use grep to find all occurences of the line and the number of the line where it appears in the file, for instance:
Code:
grep -n B.\ Gates /directory_to/filename
The (-n) will list all line numbers in the file where B. Gates is, with vi you place the cursor at the beginning of these lines and type: 'dd' to remove the line. You can also use the grep command and pipe it to "rm" to automate. Back up the file first and try on a copy as suggested above.

EDIT: Of course, both of these examples are bash and not vi, I don't think you can automate with vi. Type: 'vimtutor' in a terminal to take a little course for using vi/vim.

Last edited by Junior Hacker; 04-23-2007 at 07:04 PM.
 
Old 04-23-2007, 07:39 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Arow
Why not use sed? For example, to delete all the line containing the string B. Gates
Code:
sed "/B. Gates/d" filename
Of course, make a copy of the file first!!
And if you actually want the file changed, use the -i flag ... ;}


Cheers,
Tink
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
copy lines in vi editor jkmartha Linux - Newbie 4 05-14-2005 02:19 AM
delete some lines from a file freelinuxcpp Linux - Software 4 01-17-2004 10:28 AM
Editior automatically delete content between lines dnla Linux - Newbie 2 10-21-2003 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:57 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