Operating System - HP-UX
1753767 Members
5679 Online
108799 Solutions
New Discussion юеВ

Re: Help needed amending lots of files.

 
SOLVED
Go to solution
JoyBag_1
Frequent Advisor

Help needed amending lots of files.

Hi guys,

I have a requirement to remove certain lines from a large number of files.

The sections I need to remove are in the following format:

STRING
{
-options
-more options
}

So basically I need something that will find "STRING" and delete it and all lines down to and including the "}".

the number of "-options" can change in each file, so i can't specify deleting a fixed number of lines.

can awk do this?
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help needed amending lots of files.

Hi:

# perl -ne 'print unless /^STRING/../^}/' file

...and if you want to update in place:

# perl -ni.old -e 'print unless /^STRING/../^}/' file

...which will preserve a backup copy as ".old". Drop the ".old" and leave a bare '-i' switch and no backup copy will be made, but the update will still be done in place.

Whille 'sed' doesn't allow in place edits, you can do:

# sed -ne '/^STRING/,/^}/!p' file > file.new
# mv file.new file

Regards!

...JRF...
Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Help needed amending lots of files.

Hi (again):

Oh, and you asked specifically about 'awk'. That's quite similar, too:

# awk '/^STRING/,/^}/ {next};{print}' file

Regards!

...JRF...
JoyBag_1
Frequent Advisor

Re: Help needed amending lots of files.

Hi James,

Tnanks for all the suggestions, I've actually just found that awk solution with with a google search. It works perfectly.

Gonna have a play with perl now as I have no idea how to use it.

Thanks again.
JoyBag_1
Frequent Advisor

Re: Help needed amending lots of files.

used Jame's awk solution as it worked perfectly.