获取长文本中的文本字符串的值

时间:2018-07-10 18:54:30

标签: regex perl repeat

我需要得到以下结果。

cat sample.txt

"killPointsDelta":-6.18067741,"killStreaks":0,"kills":1
"killPointsDelta":-6.18067741,"killStreaks":0,"kills":3
"killPointsDelta":-6.18067741,"killStreaks":0,"kills":5
"killPointsDelta":-6.18067741,"killStreaks":0,"kills":0


perl -ne 'print "$&\n" if /"kills":\K[^,}]+/' sample.txt 

正确的预期结果:

1
3
5
0

到目前为止,一切都很好。但是问题是文件包含段落而不是行。

"killPlace":93,"killPoints":1326,"killPointsDelta":-18.0114288,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"ReliableNicolas","playerId":"account.95037ec4997d488b86deedf1cf479112","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":102.023,"vehicleDestroys":0,"walkDistance":45.7359657,"weaponsAcquired":1,"winPlace":96,"winPoints":1528,"winPointsDelta":-10.5121641},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"e290c65b-0ae0-4e95-b673-60695ca5c53c","attributes":{"actor":"","shardId":"xbox-eu","stats":{"DBNOs":0,"assists":0,"boosts":1,"damageDealt":125.838959,"deathType":"byplayer","headshotKills":0,"heals":1,"killPlace":50,"killPoints":1350,"killPointsDelta":-3.38219357,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"RGB3 illusionz","playerId":"account.52947057108a4f0ab90e66c4a85f8146","revives":0,"rideDistance":1201.93909,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":890.847,"vehicleDestroys":0,"walkDistance":1067.83276,"weaponsAcquired":7,"winPlace":42,"winPoints":1523,"winPointsDelta":4.073688}}},{"type":"roster","id":"1a1584cd-1088-4531-8995-8b6b2c86d112","attributes":{"stats":{"rank":39,"teamId":100015},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"34504b4e-72a0-4b27-8c2e-d00d49087019"}]}}},{"type":"roster","id":"5a062295-c1ea-41c6-97d0-b7608106b130","attributes":{"stats":{"rank":89,"teamId":100042},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"6a5311f5-be80-4fba-83ff-e03b6dd50a18"}]}}},{"type":"roster","id":"8ae96e66-2b53-4d4c-8ea9-edef1e72e4e9","attributes":{"won":"false","shardId":"xbox-eu","stats":{"rank":45,"teamId":100063}},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"a8e2fba9-ebf0-4d40-879b-b28c0b7e7d3e"}]}}},{"type":"roster","id":"c60a6439-0882-4492-a6a0-ae13d3998593","attributes":{"stats":{"rank":85,"teamId":100079},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"95659497-d1a3-41dd-8207-bc2928ecfaf6"}]}}},{"type":"participant","id":"ed50e918-e8b4-4b80-88d4-dcb2386ee3be","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":0,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":68,"killPoints":1266,"killPointsDelta":-7.962034,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"cjjannerman","playerId":"account.c4211e45f7914b4dba6857c8e4185a29","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":520.226,"vehicleDestroys":0,"walkDistance":899.256958,"weaponsAcquired":4,"winPlace":67,"winPoints":1608,"winPointsDelta":-5.16031933},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"9e4231ea-b1e5-4161-9d14-92e09ea75b49","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":66.86932,"deathType":"byplayer","headshotKills":0,"heals":5,"killPlace":57,"killPoints":1337,"killPointsDelta":-6.18067741,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"RSTHIBS","playerId":"account.0dd24316ab8e457293af9326e8e8a291","revives":0,"rideDistance":3556.051,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":734.35,"vehicleDestroys":0,"walkDistance":764.56,"weaponsAcquired":5,"winPlace":52,"winPoints":1612,"winPointsDelta":-1.36759019},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"cdfbce43-b254-44ac-aa44-6874680a2d2b","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":0,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":69,"killPoints":1433,"killPointsDelta":-15.2984076,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"CoRe X SCULLION","playerId":"account.9701b6993a304bf8b3892657aa5cd4b3","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":432.763,"vehicleDestroys":0,"walkDistance":829.7604,"weaponsAcquired":10,"winPlace":71,"winPoints":1615,"winPointsDelta":-6.362017},"actor":"","shardId":"xbox-eu"}},{"type":"roster","id":"b026d5ac-3ed7-4d4e-bdfe-e449f24ec17d","attributes":{"stats":{"rank":24,"teamId":100061},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"2ccbbdad-ef5b-4ff9-be51-c3c82d49b6c8"}]}}},{"type":"roster","id":"e529c2fb-2fa8-41fb-b23a-267c44c3e623","attributes":{"stats":{"rank":10,"teamId":100073},"won":"false","shardId":"xbox-eu"},"relationships":{"participants":{"data":[{"type":"participant","id":"90c65c8c-1ce2-40be-84e8-e1e2a14a8bab"}]},"team":{"data":null}}},{"type":"participant","id":"34504b4e-72a0-4b27-8c2e-d00d49087019","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":63.0793152,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":49,"killPoints":1156,"killPointsDelta":7.23155546,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"Jumbolicker","playerId":"account.e1f5cfe107424609927e552820c5d5ad","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":979.869,"vehicleDestroys":0,"walkDistance":1031.32593,"weaponsAcquired":2,"winPlace":39,"winPoints":1551,"winPointsDelta":4.23961639}

谢谢

3 个答案:

答案 0 :(得分:1)

完整sample.txt:

"killPointsDelta":-6.18067741,"killStreaks":0,"kills":1
"killPointsDelta":-6.18067741,"killStreaks":0,"kills":3
"killPointsDelta":-6.18067741,"killStreaks":0,"kills":5
"killPointsDelta":-6.18067741,"killStreaks":0,"kills":0
"killPlace":93,"killPoints":1326,"killPointsDelta":-18.0114288,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"ReliableNicolas","playerId":"account.95037ec4997d488b86deedf1cf479112","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":102.023,"vehicleDestroys":0,"walkDistance":45.7359657,"weaponsAcquired":1,"winPlace":96,"winPoints":1528,"winPointsDelta":-10.5121641},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"e290c65b-0ae0-4e95-b673-60695ca5c53c","attributes":{"actor":"","shardId":"xbox-eu","stats":{"DBNOs":0,"assists":0,"boosts":1,"damageDealt":125.838959,"deathType":"byplayer","headshotKills":0,"heals":1,"killPlace":50,"killPoints":1350,"killPointsDelta":-3.38219357,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"RGB3 illusionz","playerId":"account.52947057108a4f0ab90e66c4a85f8146","revives":0,"rideDistance":1201.93909,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":890.847,"vehicleDestroys":0,"walkDistance":1067.83276,"weaponsAcquired":7,"winPlace":42,"winPoints":1523,"winPointsDelta":4.073688}}},{"type":"roster","id":"1a1584cd-1088-4531-8995-8b6b2c86d112","attributes":{"stats":{"rank":39,"teamId":100015},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"34504b4e-72a0-4b27-8c2e-d00d49087019"}]}}},{"type":"roster","id":"5a062295-c1ea-41c6-97d0-b7608106b130","attributes":{"stats":{"rank":89,"teamId":100042},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"6a5311f5-be80-4fba-83ff-e03b6dd50a18"}]}}},{"type":"roster","id":"8ae96e66-2b53-4d4c-8ea9-edef1e72e4e9","attributes":{"won":"false","shardId":"xbox-eu","stats":{"rank":45,"teamId":100063}},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"a8e2fba9-ebf0-4d40-879b-b28c0b7e7d3e"}]}}},{"type":"roster","id":"c60a6439-0882-4492-a6a0-ae13d3998593","attributes":{"stats":{"rank":85,"teamId":100079},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"95659497-d1a3-41dd-8207-bc2928ecfaf6"}]}}},{"type":"participant","id":"ed50e918-e8b4-4b80-88d4-dcb2386ee3be","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":0,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":68,"killPoints":1266,"killPointsDelta":-7.962034,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"cjjannerman","playerId":"account.c4211e45f7914b4dba6857c8e4185a29","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":520.226,"vehicleDestroys":0,"walkDistance":899.256958,"weaponsAcquired":4,"winPlace":67,"winPoints":1608,"winPointsDelta":-5.16031933},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"9e4231ea-b1e5-4161-9d14-92e09ea75b49","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":66.86932,"deathType":"byplayer","headshotKills":0,"heals":5,"killPlace":57,"killPoints":1337,"killPointsDelta":-6.18067741,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"RSTHIBS","playerId":"account.0dd24316ab8e457293af9326e8e8a291","revives":0,"rideDistance":3556.051,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":734.35,"vehicleDestroys":0,"walkDistance":764.56,"weaponsAcquired":5,"winPlace":52,"winPoints":1612,"winPointsDelta":-1.36759019},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"cdfbce43-b254-44ac-aa44-6874680a2d2b","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":0,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":69,"killPoints":1433,"killPointsDelta":-15.2984076,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"CoRe X SCULLION","playerId":"account.9701b6993a304bf8b3892657aa5cd4b3","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":432.763,"vehicleDestroys":0,"walkDistance":829.7604,"weaponsAcquired":10,"winPlace":71,"winPoints":1615,"winPointsDelta":-6.362017},"actor":"","shardId":"xbox-eu"}},{"type":"roster","id":"b026d5ac-3ed7-4d4e-bdfe-e449f24ec17d","attributes":{"stats":{"rank":24,"teamId":100061},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"2ccbbdad-ef5b-4ff9-be51-c3c82d49b6c8"}]}}},{"type":"roster","id":"e529c2fb-2fa8-41fb-b23a-267c44c3e623","attributes":{"stats":{"rank":10,"teamId":100073},"won":"false","shardId":"xbox-eu"},"relationships":{"participants":{"data":[{"type":"participant","id":"90c65c8c-1ce2-40be-84e8-e1e2a14a8bab"}]},"team":{"data":null}}},{"type":"participant","id":"34504b4e-72a0-4b27-8c2e-d00d49087019","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":63.0793152,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":49,"killPoints":1156,"killPointsDelta":7.23155546,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"Jumbolicker","playerId":"account.e1f5cfe107424609927e552820c5d5ad","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":979.869,"vehicleDestroys":0,"walkDistance":1031.32593,"weaponsAcquired":2,"winPlace":39,"winPoints":1551,"winPointsDelta":4.23961639}

Perl命令:

perl -ne 'while (s/^.*?"kills":([0-9]+)//) {print("$1\n");}' sample.txt

输出:

1
3
5
0
0
0
0
0
0
0

答案 1 :(得分:0)

http://stedolan.github.io/jq/

jq '..|.kills?' xbox-match-api.json

答案 2 :(得分:-1)

这是我的答案

without perl one liner

但是@ landru27给出的答案可能最适合您的需求。

文件:sample.txt在一行中包含所有文本!

"killPlace":93,"killPoints":1326,"killPointsDelta":-18.0114288,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"ReliableNicolas","playerId":"account.95037ec4997d488b86deedf1cf479112","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":102.023,"vehicleDestroys":0,"walkDistance":45.7359657,"weaponsAcquired":1,"winPlace":96,"winPoints":1528,"winPointsDelta":-10.5121641},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"e290c65b-0ae0-4e95-b673-60695ca5c53c","attributes":{"actor":"","shardId":"xbox-eu","stats":{"DBNOs":0,"assists":0,"boosts":1,"damageDealt":125.838959,"deathType":"byplayer","headshotKills":0,"heals":1,"killPlace":50,"killPoints":1350,"killPointsDelta":-3.38219357,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"RGB3 illusionz","playerId":"account.52947057108a4f0ab90e66c4a85f8146","revives":0,"rideDistance":1201.93909,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":890.847,"vehicleDestroys":0,"walkDistance":1067.83276,"weaponsAcquired":7,"winPlace":42,"winPoints":1523,"winPointsDelta":4.073688}}},{"type":"roster","id":"1a1584cd-1088-4531-8995-8b6b2c86d112","attributes":{"stats":{"rank":39,"teamId":100015},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"34504b4e-72a0-4b27-8c2e-d00d49087019"}]}}},{"type":"roster","id":"5a062295-c1ea-41c6-97d0-b7608106b130","attributes":{"stats":{"rank":89,"teamId":100042},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"6a5311f5-be80-4fba-83ff-e03b6dd50a18"}]}}},{"type":"roster","id":"8ae96e66-2b53-4d4c-8ea9-edef1e72e4e9","attributes":{"won":"false","shardId":"xbox-eu","stats":{"rank":45,"teamId":100063}},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"a8e2fba9-ebf0-4d40-879b-b28c0b7e7d3e"}]}}},{"type":"roster","id":"c60a6439-0882-4492-a6a0-ae13d3998593","attributes":{"stats":{"rank":85,"teamId":100079},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"95659497-d1a3-41dd-8207-bc2928ecfaf6"}]}}},{"type":"participant","id":"ed50e918-e8b4-4b80-88d4-dcb2386ee3be","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":0,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":68,"killPoints":1266,"killPointsDelta":-7.962034,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"cjjannerman","playerId":"account.c4211e45f7914b4dba6857c8e4185a29","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":520.226,"vehicleDestroys":0,"walkDistance":899.256958,"weaponsAcquired":4,"winPlace":67,"winPoints":1608,"winPointsDelta":-5.16031933},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"9e4231ea-b1e5-4161-9d14-92e09ea75b49","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":66.86932,"deathType":"byplayer","headshotKills":0,"heals":5,"killPlace":57,"killPoints":1337,"killPointsDelta":-6.18067741,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"RSTHIBS","playerId":"account.0dd24316ab8e457293af9326e8e8a291","revives":0,"rideDistance":3556.051,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":734.35,"vehicleDestroys":0,"walkDistance":764.56,"weaponsAcquired":5,"winPlace":52,"winPoints":1612,"winPointsDelta":-1.36759019},"actor":"","shardId":"xbox-eu"}},{"type":"participant","id":"cdfbce43-b254-44ac-aa44-6874680a2d2b","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":0,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":69,"killPoints":1433,"killPointsDelta":-15.2984076,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"CoRe X SCULLION","playerId":"account.9701b6993a304bf8b3892657aa5cd4b3","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":432.763,"vehicleDestroys":0,"walkDistance":829.7604,"weaponsAcquired":10,"winPlace":71,"winPoints":1615,"winPointsDelta":-6.362017},"actor":"","shardId":"xbox-eu"}},{"type":"roster","id":"b026d5ac-3ed7-4d4e-bdfe-e449f24ec17d","attributes":{"stats":{"rank":24,"teamId":100061},"won":"false","shardId":"xbox-eu"},"relationships":{"team":{"data":null},"participants":{"data":[{"type":"participant","id":"2ccbbdad-ef5b-4ff9-be51-c3c82d49b6c8"}]}}},{"type":"roster","id":"e529c2fb-2fa8-41fb-b23a-267c44c3e623","attributes":{"stats":{"rank":10,"teamId":100073},"won":"false","shardId":"xbox-eu"},"relationships":{"participants":{"data":[{"type":"participant","id":"90c65c8c-1ce2-40be-84e8-e1e2a14a8bab"}]},"team":{"data":null}}},{"type":"participant","id":"34504b4e-72a0-4b27-8c2e-d00d49087019","attributes":{"stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":63.0793152,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":49,"killPoints":1156,"killPointsDelta":7.23155546,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"Jumbolicker","playerId":"account.e1f5cfe107424609927e552820c5d5ad","revives":0,"rideDistance":0,"roadKills":0,"swimDistance":0,"teamKills":0,"timeSurvived":979.869,"vehicleDestroys":0,"walkDistance":1031.32593,"weaponsAcquired":2,"winPlace":39,"winPoints":1551,"winPointsDelta":4.23961639}
#/usr/bin/perl

open(F, "./sample.txt") or print "Error opening file $!";
my $f = <F>;
close F;

my @line = split(/"playerId":/, $f);

#print scalar(@line) . "\n"; 

foreach my $line ( @line ) 
{
# "kills":0,
print "$1\n" if $line =~ /"kills":(\d+),/; 
}

结果

0
0
0
0
0
0