How to delete lines with a certain characteristic?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to delete lines with a certain characteristic?

John Culleton-3
I am looking for a one-liner, a script, whatever, that will
delete lines that have a certain characteristic. For example I
need a script/command  that will delete all lines that begin with a "\"
character. I want this to be a hands off process, just call on it
and stand back.  

Any hints?
--
John Culleton
Books with answers to marketing and publishing questions:
http://wexfordpress.com/tex/shortlist.pdf

Book coaches, consultants and packagers:
http://wexfordpress.com/tex/packagers.pdf

Reply | Threaded
Open this post in threaded view
|

Re: How to delete lines with a certain characteristic?

John (Eljay) Love-Jensen
Hi John,

>I need a script/command  that will delete all lines that begin with a "\"
character.

:g/^\\/d

Or if you meant to include the quotes...

:g/^"\\"/d

Broken down:
:    EX mode
g    global
 /   begin-delimiter
  ^  start-of-line
  \\ backslash (which has to be escaped with a backslash)
 /   end-delimiter
d    delete all matching lines

:help :g

HTH,
--Eljay

Reply | Threaded
Open this post in threaded view
|

Re: How to delete lines with a certain characteristic?

Gary Johnson
In reply to this post by John Culleton-3
On 2006-01-04, "John R. Culleton" <[hidden email]> wrote:
> I am looking for a one-liner, a script, whatever, that will
> delete lines that have a certain characteristic. For example I
> need a script/command  that will delete all lines that begin with a "\"
> character. I want this to be a hands off process, just call on it
> and stand back.  
>
> Any hints?

From the shell prompt, execute

    perl -ni -e 'print unless /^\\/' your_file_name_here

HTH,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

Re: How to delete lines with a certain characteristic?

Tim Chase-2
In reply to this post by John Culleton-3
> I am looking for a one-liner, a script, whatever, that will
> delete lines that have a certain characteristic. For example I
> need a script/command  that will delete all lines that begin with a "\"
> character. I want this to be a hands off process, just call on it
> and stand back.  

If you're looking to do this from a shell, you can use sed or grep to
avoid the overhead of starting up vim and hacking it to quit after started:

        grep -v '^\\' foo.txt > bar.txt

        sed '/^\\/d' foo.txt > bar.txt
        sed -n '/^\\/p' foo.txt > bar.txt

If you want to do it in-place, sed allows you to do that:

        sed -i '/^\\/d' foo.txt
        sed -in '/^\\/p' foo.txt

If you just want to do it within vim while making other edits, the other
solutions already given work nicely:

        :g/^\\/d

-tim




Reply | Threaded
Open this post in threaded view
|

Re: How to delete lines with a certain characteristic?

William Pursell
In reply to this post by Gary Johnson
Gary Johnson wrote:

>On 2006-01-04, "John R. Culleton" <[hidden email]> wrote:
>  
>
>>I am looking for a one-liner, a script, whatever, that will
>>delete lines that have a certain characteristic. For example I
>>need a script/command  that will delete all lines that begin with a "\"
>>character. I want this to be a hands off process, just call on it
>>and stand back.  
>>
>>Any hints?
>>    
>>
>
>>From the shell prompt, execute
>
>    perl -ni -e 'print unless /^\\/' your_file_name_here
>
>  
>
sed -n 's/^\\//p' file_name
echo -e 'g/^\\\/d\nw\nq\n' | ed file_name

Are also appealing.  This is somewhat off topic, but
there must be some ed gurus on the list.  What are
some cleaner ways to do that second example?