{"id":666,"date":"2020-01-23T18:11:24","date_gmt":"2020-01-23T18:11:24","guid":{"rendered":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/?page_id=666"},"modified":"2020-01-23T18:15:47","modified_gmt":"2020-01-23T18:15:47","slug":"regex","status":"publish","type":"page","link":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/regex\/","title":{"rendered":"Regex"},"content":{"rendered":"\n<p>Some usefull example regex instructions using <a href=\"http:\/\/perldoc.perl.org\/perlretut.html\">perl<\/a> instead of awk\/sed:<\/p>\n\n\n\n<p><br>\nrepeatedly replace &#8216;cat&#8217; with &#8216;dog&#8217; in file &#8216;filename.txt&#8217; after making a backup file &#8216;filename.txt.old&#8217;:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">perl -i.old -p -e 's\/cat\/dog\/g' .\/filename.txt\n<\/pre>\n\n\n\n<p><br>\nreplace in single lines from stream:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ echo -e \"111\\n222\\n333\" | perl -pe 's\/\\d(.*)\/[ $1 ]\/'\n[ 11 ]\n[ 22 ]\n[ 33 ]\n<\/pre>\n\n\n\n<p><br>\nreplace in multiple lines from stream:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ echo -e \"111\\n222\\n333\" | perl -0pe 's\/1(.*)3\/[ $1 ]\/s'\n[ 11\n222\n33 ]\n<\/pre>\n\n\n\n<p><br>\nrepeatedly extract from multiple lines from stream:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ echo -e \"1aaaa\\naaa2bb1cccc\\nccc2dd\" | perl -0ne 'while (\/1(.*?)2\/sg) {print \"[ $1 ]\\n\"}'\n[ \naaaa\naaa ]\n[ \ncccc\nccc ]\n<\/pre>\n\n\n\n<p>repeatedly extract from &#8216;begin&#8217; until matching &#8216;end&#8217; using counter over multiple lines:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo -e \"begin\\n  1\\n  begin\\n    2\\n  end\\n  3\\nend\\n4\\nend\" | perl -0ne 'while (\/(begin(?{$c=1})(?(?{$c&gt;0})(?:begin(?{$c++})|end(?{$c--})|.))*)\/sg) {print \"[ $1 ]\\n\"}'\n[ begin\n  1\n  begin\n    2\n  end\n  3\nend ]\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some usefull example regex instructions using perl instead of awk\/sed: repeatedly replace &#8216;cat&#8217; with &#8216;dog&#8217; in file &#8216;filename.txt&#8217; after making a backup file &#8216;filename.txt.old&#8217;: perl -i.old -p -e &#8216;s\/cat\/dog\/g&#8217; .\/filename.txt replace in single lines from stream: $ echo -e &#8220;111\\n222\\n333&#8221; | perl -pe &#8216;s\/\\d(.*)\/[ $1 ]\/&#8217; [ 11 ] [ 22 ] [ 33 ] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/wp-json\/wp\/v2\/pages\/666"}],"collection":[{"href":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/wp-json\/wp\/v2\/comments?post=666"}],"version-history":[{"count":2,"href":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/wp-json\/wp\/v2\/pages\/666\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/wp-json\/wp\/v2\/pages\/666\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/staff.fnwi.uva.nl\/b.terwijn\/wp-json\/wp\/v2\/media?parent=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}