简体   繁体   中英

how can I extract a pattern only using grep

How can we extract the contents present inside KeyProviderType tag only using grep command from the folllowing pattern?

<ContentProtectKeyProfiles-row><Name>PREM7</Name><Domain>42.0.112.121</Domain<ProfileType>4</ProfileType>
<Protocol>HTTP</Protocol><Port>80</Port><KeyProviderType>HLS-AES-128</KeyProviderType</ContentProtectKeyProfiles-row>
a@x:/tmp$ cat s.xml
<ContentProtectKeyProfiles-row> <Name>PREM7</Name> <Domain>42.0.112.121</Domain> <ProfileType>4</ProfileType> <Protocol>HTTP</Protocol> <Port>80</Port> <KeyProviderType>HLS-AES-128</KeyProviderType> </ContentProtectKeyProfiles-row>dhruv@dhruv-pathak:/tmp$ 
a@x:/tmp$ cat s.xml | grep -oe  "<KeyProviderType>.*</KeyProviderType>"
<KeyProviderType>HLS-AES-128</KeyProviderType>

Don't use grep to process XML files. Use a proper XML parser. For example, using xsh , I can just run

open in.xml ;
echo (//KeyProviderType) ;

BTW, I had to fix 2 tags that were missing > in your input.

You can try to use gnu awk (due to RS)

awk -v RS="KeyProviderType" 'NR%2==0 {gsub(/>|<\//,"");print}' file
HLS-AES-128

如果您的grep支持-P标志,则可以使用正则表达式的先行和后行。

cat s.xml | grep -oP "(?<=<KeyProviderType>).*(?=</KeyProviderType>)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM