Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


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

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


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.


Old 04-23-2007, 03:17 PM   #2
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,779
Blog Entries: 1

Rep: Reputation: 243Reputation: 243Reputation: 243
Why not use sed? For example, to delete all the line containing the string B. Gates
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: 60
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:
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
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,042
Blog Entries: 11

Rep: Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907
Originally Posted by Arow
Why not use sed? For example, to delete all the line containing the string B. Gates
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 ... ;}



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 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

All times are GMT -5. The time now is 01:18 PM.

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