我有一个文件,其中包含如下所示的行:
username1
username2
./2011-05-25-0.json.gz:{"repo":{"url":"https://api.github.dev/repos//","name":"/"},"type":"FollowEvent","public":true,"created_at":"2011-05-25","payload":{"target":{"gravatar_id":"5e17","id":144929,"repos":12,"followers":1,"login":"username3"}},"actor":{"gravatar_id":"dec","id":21,"url":"https://api.github.dev/users/user","avatar_url":"https://secure.gravatar.com/avatar/decc?d=http://github.dev%2Fimages%2Fgravatars%2Fgravatar-user-420.png","login":"username4"},"id":"14"}
./2011-05-25-0.json.gz:{"repo":{"url":"https://api.github.dev/repos//","name":"/"},"type":"FollowEvent","public":true,"created_at":"2011-05-25","payload":{"target":{"gravatar_id":"f9ed","id":82,"repos":5,"followers":4,"login":"username5"}},"actor":{"gravatar_id":"decc93","id":21,"url":"https://api.github.dev/users/username","avatar_url":"https://secure.gravatar.com/avatar/de?d=http://github.dev.user-420.png","login":"username6"},"id":"147"}
我想获取一个包含以下内容的文件:
username1
username2
username4
username6
也就是说,如果字符串"login":
出现在一行上,则在第二次出现"
之后立即获取"login"
中包含的字符串。否则,请保持原状。
如何使用grep
或sed
来做到这一点?
答案 0 :(得分:1)
您可以尝试:
sed 's/.*{.*login":"\([^"]*\)".*/\1/g' text.txt
或
sed -i.bkup 's/.*{.*login":"\([^"]*\)"}.*/\1/g' text.txt
要使用新的用户名覆盖文件,并将旧的用户名bkup到text.txt.bkup
注意:假定用户名不包含正则表达式描述的模式。