Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

unix sed: delete first ocurrence after pattern

Posted on 2011-02-17
2
Medium Priority
?
677 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

926 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