用awk或sed怎么能实现取第一个值和最后的值? 50
用awk或sed怎么能实现取第一个值和最后的值?190000020017|{{NAS-IP-Address{61.148.2.76}}{NAS-Port-Id{slot=...
用awk或sed怎么能实现取第一个值和最后的值?
190000020017|{ { NAS-IP-Address {61.148.2.76} } { NAS-Port-Id {slot=63;subslot=2;port=8;vlanid=75;vlanid2=16;} } { ai-Agreement-Id {190000020017} } { ai-Max-Session {1} } { ai-author-profile {10M@6} } { ai-RoamingLevel {1} } { Framed-IP-Address {0.0.0.0} } { ai-Service-State {1} } { ai-SvcAuthen {1003} } { ai-SvcAcct {11003} } { ai-ldap-flag {0} } { NAS-Port {0000} } { Filter-Id {0000} } { ai-UserNode {120} } {AI-Credit-Level {2}} {ai-Service-Id {8010001}} {username {190000020017}} {password {dbc6bc9494167f53}}}
我想得到190000020017 dbc6bc9494167f53 这两个值,怎么能实现。环境是Linux 或UNIX
这是一行数据 展开
190000020017|{ { NAS-IP-Address {61.148.2.76} } { NAS-Port-Id {slot=63;subslot=2;port=8;vlanid=75;vlanid2=16;} } { ai-Agreement-Id {190000020017} } { ai-Max-Session {1} } { ai-author-profile {10M@6} } { ai-RoamingLevel {1} } { Framed-IP-Address {0.0.0.0} } { ai-Service-State {1} } { ai-SvcAuthen {1003} } { ai-SvcAcct {11003} } { ai-ldap-flag {0} } { NAS-Port {0000} } { Filter-Id {0000} } { ai-UserNode {120} } {AI-Credit-Level {2}} {ai-Service-Id {8010001}} {username {190000020017}} {password {dbc6bc9494167f53}}}
我想得到190000020017 dbc6bc9494167f53 这两个值,怎么能实现。环境是Linux 或UNIX
这是一行数据 展开
1个回答
展开全部
#!/bin/bash
output()
{
cat<<EOF
190000020017|{ { NAS-IP-Address {61.148.2.76} } { NAS-Port-Id {slot=63;subslot=2;port=8;vlanid=75;vlanid2=16;} } { ai-Agreement-Id {190000020017} } { ai-Max-Session {1} } { ai-author-profile {10M@6} } { ai-RoamingLevel {1} } { Framed-IP-Address {0.0.0.0} } { ai-Service-State {1} } { ai-SvcAuthen {1003} } { ai-SvcAcct {11003} } { ai-ldap-flag {0} } { NAS-Port {0000} } { Filter-Id {0000} } { ai-UserNode {120} } {AI-Credit-Level {2}} {ai-Service-Id {8010001}} {username {190000020017}} {password {dbc6bc9494167f53}}}
EOF
}
output | awk '{
print $0
if(match($0, /username {([0-9]+)}} {password {(.+)}}}$/, adata))
print adata[1] " " adata[2]
}'
追问
文件中有几百行,这种数据。我用awk取时报错提示“包含太多字段”,只取了第一行,剩下的行没取到。如果写成脚本,后面跟要取的文件怎么实现?
追答
多少行都应该可以处理的,你的awk是gawk么?awk --version输出是什么?
你用的时候应该这样:
awk '{
print $0
if(match($0, /username {([0-9]+)}} {password {(.+)}}}$/, adata))
print adata[1] " " adata[2]
}' 文件
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询