Remove part of a file based on identifiers

here below is a part of the file

cat fileName.txt
 
NAME=APP-VA-va_mediaservices-113009-VA_MS_MEDIA_SERVER_NOT_PRESENT-S
FIXED=false
DATE= 2013-02-19 03:46:04.4
PRIORITY=HIGH
RESOURCE NAME=ccm113
 
NAME=APP-DS-ds_ha-140020-databaseReplicationFailure-S
FIXED=false
DATE= 2013-02-19 03:46:04.4
PRIORITY=LOW
RESOURCE NAME=ccm113
 
NAME=APP-MS-service_monitoring-118035-MS_DeviceDeployError-S
FIXED=false
DATE= 2013-02-19 03:46:04.4
PRIORITY=HIGH
RESOURCE NAME=ccm113
 
NAME=APP-VA-va_trunkgroup_mgr-114000-VA_TGM_TRUNK_FAILURE-S
FIXED=false
DATE= 2013-02-17 07:58:55.3
PRIORITY=HIGH
RESOURCE NAME=ccm114
 

this file is divided into blocks.. starting from Name and ending with Resource Name.... so if any block whose priority is LOW, i need to remove that block from the file... so what my final output file should have is

cat fileName.txt
 
NAME=APP-VA-va_mediaservices-113009-VA_MS_MEDIA_SERVER_NOT_PRESENT-S
FIXED=false
DATE= 2013-02-19 03:46:04.4
PRIORITY=HIGH
RESOURCE NAME=ccm113
 
NAME=APP-MS-service_monitoring-118035-MS_DeviceDeployError-S
FIXED=false
DATE= 2013-02-19 03:46:04.4
PRIORITY=HIGH
RESOURCE NAME=ccm113
 
NAME=APP-VA-va_trunkgroup_mgr-114000-VA_TGM_TRUNK_FAILURE-S
FIXED=false
DATE= 2013-02-17 07:58:55.3
PRIORITY=HIGH
RESOURCE NAME=ccm114

any help is appreciated.. thanks

Try

awk -F "=" '$1=="NAME"{for(i=1;i<=X;i++){if(!t){print A}};X=t=0}
{A[++X]=$0}
$2=="LOW"{t++}
END{for(i=1;i<=X;i++){if(!t){print A}}}' file
1 Like

If you are running on AIX, you can use the -p flag on grep to get a paragraph.

grep -pv "PRIORITY=LOW" infile > outfile

I'm not sure if this will help, as it depends on your OS, but it's neat if you have it available.

Robin
Liverpool/Blackburn,
UK

This can easy be down with awk

awk '!/LOW/' RS="\n *\n" ORS="\n\n" infile

Since your input file does have one space I did add * space,star as RS

NAME=APP-VA-va_mediaservices-113009-VA_MS_MEDIA_SERVER_NOT_PRESENT-S
FIXED=false
DATE= 2013-02-19 03:46:04.4
PRIORITY=HIGH
RESOURCE NAME=ccm113

NAME=APP-MS-service_monitoring-118035-MS_DeviceDeployError-S
FIXED=false
DATE= 2013-02-19 03:46:04.4
PRIORITY=HIGH
RESOURCE NAME=ccm113

NAME=APP-VA-va_trunkgroup_mgr-114000-VA_TGM_TRUNK_FAILURE-S
FIXED=false
DATE= 2013-02-17 07:58:55.3
PRIORITY=HIGH
RESOURCE NAME=ccm114


1 Like

thanks a lot pamu.. it worked :slight_smile:

---------- Post updated at 03:36 PM ---------- Previous update was at 01:38 PM ----------

Thanks jotne .. your code worked fine too :slight_smile: