Operating System - HP-UX
1748102 Members
4916 Online
108758 Solutions
New Discussion

Re: How can I delete entire lines based on a search expression in vi?

 
SOLVED
Go to solution
Eric Antunes
Honored Contributor

How can I delete entire lines based on a search expression in vi?

I need to remove the following lines from a file:

 

<AccountType>Some stuffs in here also</AccountType>

 

How can I do this with vi or another hpux program?

 

Thank you,

 

Eric Antunes

Each and every day is a good day to learn.
21 REPLIES 21
H.Merijn Brand (procura
Honored Contributor
Solution

Re: How can I delete entire lines based on a search expression in vi?

Assuming the (XML like) lines are all on a single line each:

 

In vi

 

  :g/<AccountType>/d

 

With perl:

 

  $ perl -ni -e'm/ <AccountType>/ or print' file.txt

 

But that is only safe if there is onlo what you show on a single line. If text folds and the closing tag is on the next line, or if other text is on the same line, this is going to fail.

 

Is the whole file valid HTML? or XML? 

Enjoy, Have FUN! H.Merijn
Eric Antunes
Honored Contributor

Re: How can I delete entire lines based on a search expression in vi?

Hi Merijn,

It worked! This is a valid XML file but I need to remove that part in the entire file.

Thank you,

Eric
Each and every day is a good day to learn.
Dennis Handly
Acclaimed Contributor

Re: How can I delete entire lines based on a search expression in vi?

><AccountType>Some stuff in here also</AccountType>

 

You can be more pedantic:

:g:<AccountType>.*</AccountType>:d

 

If you need to anchor the lines to beginning and end:

:g:^<AccountType>.*</AccountType>$:d

 

And you can use these in sed.

H.Merijn Brand (procura
Honored Contributor

Re: How can I delete entire lines based on a search expression in vi?

If you want to be more pedantic, please escape the inside slash :)

:g/<AccountType>.*<\/AccountType>/d

If you need to anchor the lines to beginning and end:

 

Assuming \s is whitespace in your editor, and it supports * for greedy and ? non-greedy modifiers


:g/^\s*<AccountType>.*?<\/AccountType>\s*$/d

Otherwise greedy .* might get way to much :)

 

And you can (maybe) use these in sed (depending on what version od sed you have).

Enjoy, Have FUN! H.Merijn
Dennis Handly
Acclaimed Contributor

Re: How can I delete entire lines based on a search expression in vi?

>If you want to be more pedantic, please escape the inside slash :)

 

Oops, done, replaced by a colon ":".  :-(

Eric Antunes
Honored Contributor

Re: How can I delete entire lines based on a search expression in vi?

I don't need to be pedantic. :-)

 

Thank you guys.

 

 

Each and every day is a good day to learn.
James R. Ferguson
Acclaimed Contributor

Re: How can I delete entire lines based on a search expression in vi?


@Eric Antunes wrote:

I don't need to be pedantic. :-)


Learning precision when deploying regular expressions will keep you from matching when you *don't* want to match as well as allow you to match when you should.  Form the good habits early and save yourself countless hours of pain.

 

Regards!

 

...JRF...

Eric Antunes
Honored Contributor

Re: How can I delete entire lines based on a search expression in vi?

Hi again,

 

I didn't need to be pedantic since this is a XML file with serious parse errors. So, things like <AccountType>,<EMail/>, <Contact/>... could safely be removed. But now it gets harder (at least for me, a DBA): I need to add stuffs to the file.

 

For example, I would like to add the line "<SelfBillingIndicator>0</SelfBillingIndicator>" before each line "</Customer>"

 

How can I do that in vi?

 

Thank you,

 

Eric

Each and every day is a good day to learn.
James R. Ferguson
Acclaimed Contributor

Re: How can I delete entire lines based on a search expression in vi?


@Eric Antunes wrote:

For example, I would like to add the line "<SelfBillingIndicator>0</SelfBillingIndicator>" before each line "</Customer>"

 

How can I do that in vi?

 


Using 'vi' search for the first line with "Customer":

 

Insert your "SelfbillingIndicator" line above it by (O)pening a new line above it.

 

After you have inserted the new line, yank (yy) a copy of it.

 

Hit 'n' to continue to search forward for "Customer" and when you reach a matching line, use 'P' to put the yanked buffer above the "Customer" line.

 

Regards!

 

...JRF...