Reputation: 113
I have a list of data as follows:
Account Number: 11111
Domain : domain.com
Quantity: 1
Quantity: 2
Processor: Intel Xeon E5-1650 V2 3.5GHZ, Hexa Core
SERVERCHASSIS: Standard - Single PSU - No Hot Swap Bays
Account Number: 54321
Domain : domain0.com
Quantity: 1
Processor: Intel Xeon E3-1240 V1 3.3Ghz, Quad Core
SERVERCHASSIS: Standard - Single PSU - No Hot Swap Bays
Quantity: 1
Account Number: 12345
Domain : domain1.com
Quantity: 1
I would like to use sed/awk to delete all entries of "Quantity: X" that are not followed by "Processor:" on the next line. I would also like to remove "Account Number: XXXXX" and "Domain:" lines if the lines following do not contain both "Quantity: X" and "Processor:". This in turn would change the above data to:
Account Number: 11111
Domain : domain.com
Quantity: 2
Processor: Intel Xeon E5-1650 V2 3.5GHZ, Hexa Core
SERVERCHASSIS: Standard - Single PSU - No Hot Swap Bays
Account Number: 54321
Domain : domain0.com
Quantity: 1
Processor: Intel Xeon E3-1240 V1 3.3Ghz, Quad Core
SERVERCHASSIS: Standard - Single PSU - No Hot Swap Bays
Can anyone provide a way to complete this task using sed or awk or a combination of both?
Upvotes: 0
Views: 310
Reputation: 8671
EDIT: Erf... Should have read the question before answering.
This one should work, hopefully
BEGIN {
OFS=RS
FS="\n"
RS= ""
}
{
selected = 0
drop = 0
for (i = 1; i <= NF ; i++)
{
if ($i ~ "Quantity:")
{
if ($(i+1) ~ "Processor:") selected = 1
else drop++
}
$i = $(i+drop)
}
if (selected) print
}
cmd
gawk -f processor.awk processor.txt
output
Account Number: 11111
Domain : domain.com
Quantity: 2
Processor: Intel Xeon E5-1650 V2 3.5GHZ, Hexa Core
SERVERCHASSIS: Standard - Single PSU - No Hot Swap Bays
Account Number: 54321
Domain : domain0.com
Quantity: 1
Processor: Intel Xeon E3-1240 V1 3.3Ghz, Quad Core
SERVERCHASSIS: Standard - Single PSU - No Hot Swap Bays
Upvotes: 1
Reputation: 204558
$ cat tst.awk
BEGIN{ RS=""; FS="\n" }
/Quantity:/ && /Processor:/ {
for (i=1; i<=NF; i++) {
if ( ! (($i ~ /Quantity:/) && ($(i+1) !~ /Processor:/)) ) {
print $i
}
}
print ""
}
$
$ awk -f tst.awk file
Account Number: 11111
Domain : domain.com
Quantity: 2
Processor: Intel Xeon E5-1650 V2 3.5GHZ, Hexa Core
SERVERCHASSIS: Standard - Single PSU - No Hot Swap Bays
Account Number: 54321
Domain : domain0.com
Quantity: 1
Processor: Intel Xeon E3-1240 V1 3.3Ghz, Quad Core
SERVERCHASSIS: Standard - Single PSU - No Hot Swap Bays
Upvotes: 2