질문: EPS_PROFILE_ID, MMEFEATURELIST의 조합이 있는 340039 등으로 시작하는 모든 IMSI 목록이 필요합니다.
입력하다
<BEGINFILE>
<SUBBEGIN
IMSI=340039XXXXXXXXX;
MSISDN=59995171393;
EPS_PROFILE_ID=1;
PLMNTPLID=1;
SUB_DYN_EPS_ID=1;
MMEHOST=XXXXXXXXXXXXXXXXXXXXXXXXXX.VVV.VV;
MMEREALM=dwcxd.dnhjxcdws.cdnjkcd;
EPS_IMEI=155186158551851658;
EPS_IMEISV=01;
MMEFEATURELIST=ALL_APN;
MMETIMESTAMP=15513535;
S4SGSNTIMESTAMP=0;
HLR_INDEX=1;
LTEAUTOPROV=NO;
MPS=NO;
RELAY=NO;
<SUBEND
<SUBBEGIN
IMSI=340038XXXXXXXXX;
MSISDN=59995171393;
PLMNTPLID=1;
CURRENTNAM=BOTH;
CAT=COMMON;
TBS=TS11&TS12&TS21&TS22&TS61&BS26&BS2G;
VLRLIST=2;
SGSNLIST=2;
<SUBEND
<ENDFILE>
원하는 출력
IMSI=340039XXXXXXXXX;EPS_PROFILE_ID=1;MMEFEATURELIST=ALL_APN;
답변1
나는 perl
이런 종류의 분석을 선호합니다
perl -ne '
$/ = "\n<SUBBEGIN\n"; # Read blocks starting with this pattern
($i) = /\b(IMSI=340039[^;]*)/; # Get IMSI and value
($e) = /\b(EPS_PROFILE_ID=[^;]+)/; # Get EPS_PROFILE_ID and value
($m) = /\b(MMEFEATURELIST=[^;]+)/; # Get MMEFEATURELIST and value
print "$i;$e;$m;\n" if $i && $e && $m; # Print result only if all three items are present
' input_file
예제의 출력
IMSI=340039XXXXXXXXX;EPS_PROFILE_ID=1;MMEFEATURELIST=ALL_APN;
답변2
POSIX awk를 사용하십시오.
$ cat tst.awk
BEGIN { FS="[[:space:]=]*"; OFS=";" }
/=/ { tag2val[$2] = $3 }
/<SUBEND/ {
if ( (tag2val["IMSI"] ~ /^340039/) &&
("EPS_PROFILE_ID" in tag2val) &&
("MMEFEATURELIST" in tag2val) ) {
print p("IMSI"), p("EPS_PROFILE_ID"), p("MMEFEATURELIST")
}
delete tag2val
}
function p(tag) { return tag "=" tag2val[tag] }
$ awk -f tst.awk file
IMSI=340039XXXXXXXXX;;EPS_PROFILE_ID=1;;MMEFEATURELIST=ALL_APN;
답변3
GNU sed
확장 정규식 모드(-E)를 사용하여 문제를 해결하는 한 가지 방법은 다음과 같습니다.
## holds the keys to be printed
var='IMSI EPS_PROFILE_ID MMEFEATURELIST'
sed -En '
1{:1;x;s/.*/ '"$var"' /;x;}
/^<SUBBEGIN$/,/^<SUBEND$/G
/^\s+((\S+)=\S+)\n(.*) \2(\s)/!d
s//\3\n\1\4/
/(.*\n){3}/!{h;d;}
/^\nIMSI=340039/s/\n|\s$//gp
s/.*/<SUBEND/;b1
' file
IMSI=340039XXXXXXXXX;EPS_PROFILE_ID=1;MMEFEATURELIST=ALL_APN;
미리보기를 사용하여 정규식에서 어설션을 사용합니다.
perl -lne '
push(@A, $_)
if my $e = /^<SUBBEGIN$/ ... /^<SUBEND$/;
print($1, $2, $3)
if $e =~ /E0/ && join($/, splice(@A)) =~
m/(?:
(?=$p(IMSI=340039$q))
(?=$p(EPS_PROFILE_ID=$q))
(?=$p(MMEFEATURELIST=$q))
)/xms;
BEGIN {
$q = qr/ (?:(?!;)\S)+; /xms;
$p = qr/ \A.*\n\h* /xms;
}' file
## keys provided as env to awk
s="IMSI EPS_PROFILE_ID MMEFEATURELIST" \
awk -F '^[[:blank:]]+|=' '
BEGIN {
n = split(ENVIRON["s"],a," ")
for (var in a) b[a[var]] = var
}
/^<SUBBEGIN$/,/^<SUBEND$/ {
if ( /=/ && $2 in b) {
h[$2] = $0
sub(FS, "", h[$2])
}
if ( /^<SUBEND$/ ) {
t=$0;$0=""
for (i=1; a[i] in h; i++) $i = h[a[i]]
split("", h, "=")
f = (NF == n) && ($1 ~ /=340039/)
}
}
f;{f=0}
' OFS= file
답변4
awk '/IMSI=340039/&&/EPS_PROFILE_ID/&&/MMEFEATURELIST/' filename