这个在两种模式之间打印
printf "/n"| openssl s_client -showcerts -connect www.google.com:443 | awk '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/'
然后这个删除第一个匹配集,然后打印所有多余的垃圾
printf "/n"| openssl s_client -showcerts -connect www.google.com:443 | awk '/-----BEGIN CERTIFICATE-----/{f=1;++c} !(f && c==2); /-----END CERTIFICATE-----/{f=0}'
我想得到第二个结果,除了模式匹配之外我只能使用两个awk。
printf "/n"| openssl s_client -showcerts -connect www.google.com:443 | awk '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/' | awk '/-----BEGIN CERTIFICATE-----/{f=1;++c} !(f && c==2); /-----END CERTIFICATE-----/{f=0}'
但如果有可能,我想在一个地方做。
答案 0 :(得分:1)
这似乎与this question非常相似,我的调整答案如下:
sed -n '/-----BEGIN CERTIFICATE----/,/-----END CERTIFICATE-----/ { // { x; s/$/./; x; }; x; /.../ { x; p; x; }; x; }' filename
那是
/-----BEGIN CERTIFICATE----/,/-----END CERTIFICATE-----/ {
// {
x
s/$/./ # keep a counter of boundary lines in the hold buffer
x
}
x # inspect the counter
/.../ { # if counter >= 3
x
p # print the line
x
}
x
} # with -n, falling off the end here will not lead to printing.
或者,我能想到的最好的awk是
awk '/----BEGIN CERTIFICATE----/ { flag = 1; ++ctr } flag && ctr >= 2 { print } /-----END CERTIFICATE-----/ { flag = 0 }' filename
更可读:
/----BEGIN CERTIFICATE----/ { # beginning of a range:
flag = 1 # raise flag that we're in one
++ctr # count in which one
}
flag && ctr >= 2 { print } # print only if in a range and not in the first
/-----END CERTIFICATE-----/ { # when leaving
flag = 0 # lower flag
}