Solved

unix sed: delete first ocurrence after pattern

Posted on 2011-02-17
2
671 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

734 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