读取文件直到匹配模式

时间:2017-09-17 09:50:10

标签: bash shell

我正在阅读一个文件,我希望得到这些信息,直到找到匹配为止。

将文件放在https://ufile.io/182kx

我想从lastActiveTimes获得json信息:直到找到,chatNotif:0

返回

{"707514313":1505610703,"1568212945":1505638160,"732898933":1505638352,"100009336847960":1505635266,"721251435":1505570865,"718844397":1505623246,"1461941075":1505501435,"100004389551456":1505637706,"1211838231":1505582601,"1040249145":1505636186,"1242203773":1505628782,"517814298":1505567030,"807572767":1505638353,"738307936":1505638009,"683874946":1505598251,"822469152":1505636589,"727476234":1505627000,"781209703":1505631577,"1058918804":1505629365,"539657070":1505629599,"1506662943":1505606109,"538279690":1505575467,"1122078957":1505633239,"1426504238":1505614371,"1760126206":1505637897,"100009494169236":1505633218,"100000193088625":1505633785,"628050112":1505599301,"692803720":1505602132,"100000982526361":1505611187,"1567918281":1505549275,"562061542":1505633121,"680188549":1505637979,"201400626":1505510516,"709905371":1505635235,"100000921265645":1505637511,"100002576634271":1505633420,"100001152648289":1505638358,"1580474418":1505583268,"1093906498":1505635647,"1568491642":1505613600,"1759941492":1505592915,"1021502749":1505621933,"100001091369712":1505593740,"1201111516":1505631603,"511729394":1505637150,"1228064980":1505627119,"1484357891":1505632720,"773982263":1505636776,"610763631":1505581711,"581839860":1505636663,"100001509228647":1505550106,"100001496847848":1505520708,"553024640":1505631903,"1657607627":1505460838,"100008134920032":1505636261,"518105631":1505610763,"100000167522595":1505559871,"604094302":1505591423,"831534764":1505498705,"716402163":1505625063,"100005862197805":1505615273,"779160397":1505625381,"683029723":1505602056,"1105801871":1505638150,"1007323327":1505618323,"500432034":1505617899,"1019441248":1505593648,"1321064988":1505549642,"600465009":1505557526,"734790522":1505614982,"1139898038":1505597330,"762749332":1505595541,"100006926654236":1505637009,"100007887856728":1505580453,"1073032118":1505602788,"575893114":1505630287,"1463373342":1505609305}

我正在尝试使用

sed -n '/lastActiveTimes:/,/chatNotif/p' home.html | sed '1s/.*lastActiveTimes://; $s/chatNotif.*//' > end.json

但没有工作

2 个答案:

答案 0 :(得分:1)

使用GNU grep和Perl regular expression-P):

grep -Poz '(?<=lastActiveTimes:).*(\n.*)*(?=,chatNotif)' file

输出:

  

{&#34; 707514313&#34;:1505610703,&#34; 1568212945&#34;:1505639008,&#34; 732898933&#34;:1505641310,&#34; 100009336847960&#34;:1505641325,&# 34; 721251435&#34;:1505570865,&#34; 718844397&#34;:1505623246,&#34; 1461941075&#34;:1505501435,&#34; 100004389551456&#34;:1505637706,&#34; 1211838231&#34; :1505582601&#34; 1040249145&#34;:1505639741,&#34; 1242203773&#34;:1505628782,&#34; 517814298&#34;:1505567030,&#34; 807572767&#34;:1505638510,&#34 ; 738307936&#34;:1505641007,&#34; 683874946&#34;:1505598251,&#34; 822469152&#34;:1505636589,&#34; 727476234&#34;:1505627000,&#34; 781209703&#34 ;: 1505631577&#34; 1058918804&#34;:1505629365,&#34; 539657070&#34;:1505629599,&#34; 1506662943&#34;:1505606109,&#34; 538279690&#34;:1505640516,&#34; 1122078957&#34;:1505633239,&#34; 1426504238&#34;:1505614371,&#34; 1760126206&#34;:1505637897,&#34; 100009494169236&#34;:1505633218,&#34; 100000193088625&#34;:1505633785 &#34; 628050112&#34;:1505599301,&#34; 692803720&#34;:1505641333,&#34; 100000982526361&#34;:1505611187,&#34; 1567918281&#34;:15055 49275&#34; 562061542&#34;:1505641305,&#34; 680188549&#34;:1505637979,&#34; 201400626&#34;:1505510516,&#34; 709905371&#34;:1505635235,&#34; 100000921265645&#34;:1505637511,&#34; 100002576634271&#34;:1505633420,&#34; 100001152648289&#34;:1505640582,&#34; 1580474418&#34;:1505583268,&#34; 1093906498&#34;:1505635647 &#34; 1568491642&#34;:1505638670,&#34; 1759941492&#34;:1505592915,&#34; 1021502749&#34;:1505621933,&#34; 100001091369712&#34;:1505593740,&#34; 1201111516& #34;:1505631603,&#34; 511729394&#34;:1505637150,&#34; 1228064980&#34;:1505627119,&#34; 1484357891&#34;:1505632720,&#34; 773982263&#34;:1505641308, &#34; 610763631&#34;:1505581711,&#34; 581839860&#34;:1505641241,&#34; 100001509228647&#34;:1505550106,&#34; 100001496847848&#34;:1505520708,&#34; 553024640&# 34;:1505631903,&#34; 1657607627&#34;:1505460838,&#34; 100008134920032&#34;:1505636261,&#34; 518105631&#34;:1505610763,&#34; 100000167522595&#34;:1505559871,& #34; 604094302&#34;:1505591423,&#34; 831534764&#34;:1505498705,&#34; 716402163&#34;:1505625063,&#34; 10 0005862197805&#34;:1505615273,&#34; 779160397&#34;:1505625381,&#34; 683029723&#34;:1505602056,&#34; 1105801871&#34;:1505641175,&#34; 1007323327&#34;:1505640781 &#34; 500432034&#34;:1505617899,&#34; 1019441248&#34;:1505593648,&#34; 1321064988&#34;:1505549642,&#34; 600465009&#34;:1505557526,&#34; 734790522& #34;:1505614982,&#34; 1139898038&#34;:1505597330,&#34; 762749332&#34;:1505595541,&#34; 100006926654236&#34;:1505637009,&#34; 100007887856728&#34;:1505580453, &#34; 1073032118&#34;:1505602788,&#34; 575893114&#34;:1505630287,&#34; 1463373342&#34;:1505640415}

答案 1 :(得分:1)

如果您不介意使用 Perl ,可以尝试:

perl -lne 'print $& if /(?<=lastActiveTimes:).*?(?=,chatNotif)/g' home.txt  

它在这两个断言之间打印任何内容:lastActiveTimes:,chatNotif

ack -o '(?<=lastActiveTimes:).*?(?=,chatNotif)' home.txt