我有这个文件
Array ( [0] => ROLE_ADMIN@READ@User [1] => ROLE_ADMIN_DD_USER@READ@User [2] => ROLE_DD_USER@READ@* [3] => ROLE_USER@READ@User [4] => IS_AUTHENTICATED_ANONYMOUSLY@*@User [5] => *@READ@User [6] => ROLE_ADMIN@form@User [7] => *@form@User [8] =>
我想提取ROLE_ADMIN@READ@User
部分然后用逗号分隔。
[ ROLE_ADMIN@READ@User , ROLE_ADMIN@READ@User ]
我在sed
sed -re 's/ ([A-Z_\*]*)@([A-Za-z\*]*)@[a-z\*]*/#/g' role.txt
这已经完成了一半;我不知道下一步该做什么。你能帮忙吗?
答案 0 :(得分:1)
不是试图在=>
之后捕获值,而是更容易替换其他所有内容:
sed -e 's/.* \[0] => /[ /' -e 's/\[[0-9]*] =>/,/g' -e 's/[, ]*$/]/' role.txt
说明:
[
,
]
这样你最终得到:
[ROLE_ADMIN @READ @ User,ROLE_ADMIN_DD_USER @ READ @ User,ROLE_DD_USER @ READ @ *,ROLE_USER @ READ @ User,IS_AUTHENTICATED_ANONYMOUSLY @ * @ User,* @ READ @ User,ROLE_ADMIN @ form @ User,* @ form @用户]
如果您想对条目进行排序,我建议改为perl
:
perl -ne '@u = m/=> *(\S+)/g; print "[ ", join(" , ", sort(@u)), " ]\n"' < role.txt
这会给你:
[* @ READ @ User,* @ form @ User,IS_AUTHENTICATED_ANONYMOUSLY @ * @ User,ROLE_ADMIN @ READ @ User,ROLE_ADMIN @ form @ User,ROLE_ADMIN_DD_USER @ READ @ User,ROLE_DD_USER @ READ @ *,ROLE_USER @READ @用户]