Solved

unix sed: delete first ocurrence after pattern

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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.

777 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