LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-19-2004, 04:00 PM   #1
geomatt
Member
 
Registered: Mar 2004
Location: PA
Distribution: Slackware 12.0
Posts: 315

Rep: Reputation: 30
vi command to find and delete all lines beginning with a character


Anyone know how I can delete all of the lines in a file that begin with some arbitary character?
Been poring over the vi documentation and various online sources of info, but I can't figure it out....

-geo
 
Old 12-19-2004, 04:06 PM   #2
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
You could always use Grep in reverse mode, within or without Vim, such as
Code:
grep -vE ^# inputfile > outputfile
where # is/are your desired character(s) at the start of the line...

Cheers,

mj
 
Old 12-19-2004, 04:12 PM   #3
geomatt
Member
 
Registered: Mar 2004
Location: PA
Distribution: Slackware 12.0
Posts: 315

Original Poster
Rep: Reputation: 30
Hey great thanks. Doesn't need to be within vi as long as it works.

-geo
 
Old 12-19-2004, 04:14 PM   #4
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Great

If you're a real Vim afficionado, you would of course just run it from within the shell mode of Vim.

Cheers,

mj
 
Old 12-19-2004, 07:04 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Just use ex mode -

:g/^x.*$/d

with 'x' being your arbitrary character.
 
Old 12-19-2004, 07:19 PM   #6
geomatt
Member
 
Registered: Mar 2004
Location: PA
Distribution: Slackware 12.0
Posts: 315

Original Poster
Rep: Reputation: 30
Thanks. I'll try that too. Just starting to learn a bit more about what can be done in vi....
-geo
 
Old 12-19-2004, 09:12 PM   #7
SlowMindThinkin
Member
 
Registered: Sep 2004
Posts: 144

Rep: Reputation: 15
A decade or so ago, I was pretty good with vi. I do not believe you can do what you want with vi. You might be able to with sed, however.
 
Old 12-19-2004, 09:43 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Quote:
:g/^x.*$/d
The method which digiot shows works quite well.
 
Old 12-20-2004, 03:30 AM   #9
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Speaking of sed, I actually messed that up slightly -

:g/^x/d

is all that's necessary. d will delete all of the addressed lines, so the rest is unnecessary - I guess I put that from habit because I use sed and 's' a lot. Either one works, but it's wasted typing in this case.
 
  


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
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
Help, help! How to find lines in between searched patterns? wujee Linux - Software 1 01-06-2005 02:52 PM
How to delete a file beginning with '-'? zoomzoom Linux - General 2 02-02-2004 10:01 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 - Software

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