Solved

unix sed: delete first ocurrence after pattern

Posted on 2011-02-17
2
666 Views
Last Modified: 2012-05-11
Dear Experts,

In a file I need to delete lines.
Each line in which there is PRIORITY must be removed but only if it is first occurence after the word SCHEDULE.

So if I have this:

SCHEDULE WKS#SOTS-TEST
PRIORITY 85
:
WKS#OTS-EXIT0-CMD
PRIORITY 20
END

SCHEDULE WKS#SOTS-TEST1
ON RUNCYCLE TC TC-SUTH
PRIORITY 70
:
WKS#OTS-EXIT1-CMD
PRIORITY 30
END

Should becomes this...

SCHEDULE WKS#SOTS-TEST
ON RUNCYCLE TC TC-SUTH
:
WKS#OTS-EXIT0-CMD
PRIORITY 20
END

SCHEDULE WKS#SOTS-TEST1
:
WKS#OTS-EXIT1-CMD
PRIORITY 30
END

An other way to see it is that every line that contains PRIORITY between [SCHEDULE] and [:]
is to be deleted.

The goal here is to remove priorities set on schedule level.
Thanks again for your appreciated help.
0
Comment
Question by:Watnog
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 34915317
Hi,

how about (again) awk?

awk '{if ($0~"^SCHEDULE") {SF="Y"}; if ($0~"^:") {SF="N"}; if ($0~"^PRIORITY" && SF=="Y") {} else {print}}' inputfile

Please note that with the above "PRIORITY", the colon ":" and "SCHEDULE" must all start in column 1!
If this is not desired remove "^", like:

awk '{if ($0~"SCHEDULE") {SF="Y"}; if ($0~":") {SF="N"}; if ($0~"PRIORITY" && SF=="Y") {} else {print}}' inputfile

wmp
0
 

Author Closing Comment

by:Watnog
ID: 34915506
Excellent.
Thank you wmp!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now