LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   vi command to find and delete all lines beginning with a character (https://www.linuxquestions.org/questions/linux-software-2/vi-command-to-find-and-delete-all-lines-beginning-with-a-character-268140/)

geomatt 12-19-2004 04:00 PM

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

mjrich 12-19-2004 04:06 PM

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

geomatt 12-19-2004 04:12 PM

Hey great thanks. Doesn't need to be within vi as long as it works.

-geo

mjrich 12-19-2004 04:14 PM

Great :)

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

Cheers,

mj

slakmagik 12-19-2004 07:04 PM

Just use ex mode -

:g/^x.*$/d

with 'x' being your arbitrary character.

geomatt 12-19-2004 07:19 PM

Thanks. I'll try that too. Just starting to learn a bit more about what can be done in vi....
-geo

SlowMindThinkin 12-19-2004 09:12 PM

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.

homey 12-19-2004 09:43 PM

Quote:

:g/^x.*$/d
The method which digiot shows works quite well.

slakmagik 12-20-2004 03:30 AM

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. :)


All times are GMT -5. The time now is 02:26 PM.