我有一个数据列表如下:
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
我想使用sed / awk删除下一行中未跟随“Processor:”的“Quantity:X”的所有条目。如果以下行不包含“Quantity:X”和“Processor:”,我还想删除“帐号:XXXXX”和“域:”行。这反过来会将上述数据更改为:
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
任何人都可以使用sed或awk或两者的组合提供完成此任务的方法吗?
答案 0 :(得分:2)
$ 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
答案 1 :(得分:1)
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
输出
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