Solved

unix sed: delete first ocurrence after pattern

Posted on 2011-02-17
2
672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

626 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