{"id":46,"date":"2018-07-24T12:06:37","date_gmt":"2018-07-24T12:06:37","guid":{"rendered":"http:\/\/suspensao.blog.br\/disbelief\/?p=46"},"modified":"2018-07-24T20:25:58","modified_gmt":"2018-07-24T20:25:58","slug":"exchanging-sticker-cards-with-the-terminal","status":"publish","type":"post","link":"https:\/\/suspensao.blog.br\/disbelief\/exchanging-sticker-cards-with-the-terminal\/","title":{"rendered":"Exchanging World Cup&#8217;s sticker figures with the terminal"},"content":{"rendered":"<p>One of my hobbies during this recent World Cup was to collect stickers. Actually, I\u2019ve built the sticker album because my son wanted it but I had fun, too, I guess.<\/p>\n<figure id=\"attachment_333\" aria-describedby=\"caption-attachment-333\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/suspensao.blog.br\/descrenca\/wp-content\/uploads\/2018\/07\/IMG_20180716_090001719.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-333\" src=\"https:\/\/i0.wp.com\/suspensao.blog.br\/descrenca\/wp-content\/uploads\/2018\/07\/IMG_20180716_090001719-300x169.jpg?resize=300%2C169\" alt=\"2018 sticker album showing France team missing three pictures.\" width=\"300\" height=\"169\" \/><\/a><figcaption id=\"caption-attachment-333\" class=\"wp-caption-text\">Sadly, not completed yet<\/figcaption><\/figure>\n<p>An important part of collecting stickers is to exchange the repeated ones. Through messages in WhatsApp groups, we report which repeated stickers we have and which ones we still need. As a programmer, I refused to compare the lists myself, so I wrote a <a href=\"https:\/\/pastebin.com\/FLDz684P\">little program<\/a> em Python (with <a href=\"http:\/\/suspensao.blog.br\/disbelief\/doctest\/\">doctests and all<\/a>) to find intersections.<\/p>\n<h3>The missing laptop<\/h3>\n<p>Last week, a person came to my home to exchange stickers. I had the lists of repeated and needed cards, both mine and hers, but my script was in another laptop. I did not even know where this machine was and my guest was in a hurry.<\/p>\n<p>There was no time to find the computer, or rewriting the program. Or even to compare manually.<\/p>\n<p>It&#8217;s Unix time!<\/p>\n<h3>The list format<\/h3>\n<p>In general, the lists had this format:<\/p>\n<p><code>15, 18, 26, 31, 40, 45 (2), 49, 51, 110, 115, 128, 131 (2), 143, 151, 161, 162, 183 (2), 216 (2), 221, 223, 253, 267 (3), 269, 280, 287, 296, 313, 325, 329, 333 (2), 353 (3), 355, 357, 359, 362, 365, 366, 371, 373, 384, 399, 400, 421 (2), 445, 457, 469, 470, 498 (2), 526, 536, 553, 560, 568, 570, 585, 591 (2), 604 (2), 639 (2), 660.<\/code><\/p>\n<p>Basically, I needed to remove everything which were not digits, alongside with the numbers in parentheses, and to compare both lists. Easy, indeed.<\/p>\n<h3>Pre-processing with sed<\/h3>\n<p>First, I had to remove the counters between parentheses:<\/p>\n<p><code>$ cat list.txt | sed 's\/([^)]*)\/\/g'<br \/>\n15, 18, 26, 31, [...] 591 , 604 , 639 , 660.<\/code><\/p>\n<p>(I know, <a href=\"http:\/\/porkmail.org\/era\/unix\/award.html\">UUOC<\/a>. Whatever.)<\/p>\n<p>Then, I put each number in its own line:<\/p>\n<p><code>$ cat list.txt | sed 's\/([^)]*)\/\/g' | sed 's\/, *\/\\n\/g'<\/code><\/p>\n<p>Later, I clean up every line removing any character that is not a digit:<\/p>\n<p><code>cat list.txt | sed 's\/([^)]*)\/\/g' | sed 's\/, *\/\\n\/g' | sed 's\/[^0-9]*\\([0-9]*\\)[^0-9]*\/\\1\/g'<\/code><\/p>\n<p>In practice, I only call <code>sed<\/code> once, passing up both expressions. Here, I believe it would be clearer to invoke <code>sed<\/code> many times.)<\/p>\n<p>Finally, I sort the values:<\/p>\n<p><code>$ cat list.txt | sed 's\/([^)]*)\/\/g' | sed 's\/, *\/\\n\/g' | sed 's\/[^0-9]*\\([0-9]*\\)[^0-9]*\/\\1\/g' | sort -n &gt; mine-needed.txt<\/code><\/p>\n<p>I do it with the list of needed stickers, and also with the list of repeated stickers, getting two files.<\/p>\n<h3>Finding intersections with grep<\/h3>\n<p>Now, I need to compare them. There are many options, and I choose to <a href=\"https:\/\/stackoverflow.com\/a\/27960271\/287976\">use <code>grep<\/code><\/a>.<\/p>\n<p>In this case, I called <code>grep<\/code> with one of the files as an input, and the other file as a list of patterns to match, through the <code>-f<\/code> option. Also, only the complete match matters here, so we are going to use the <code>-x<\/code> flag. Finally, I asked <code>grep<\/code> to compare strings directly (instead of treating them as regular expressions) with the <code>-F<\/code> flag.<\/p>\n<p><code>$ fgrep -Fxf mine-needed.txt theirs-repeated.txt<br \/>\n253<br \/>\n269<br \/>\n333<br \/>\n470<br \/>\n639<\/code><\/p>\n<p>Done! In a minute, I already know which stickers I want. I just need to do the same with my repeated ones.<\/p>\n<h3>Why is this interesting?<\/h3>\n<p>These one-liners are not really a big deal to me, today. The interesting thing is that when I started to use the terminal, they would be incredible. Really, look how many pipes we use to pre-process the files! And this <code>grep<\/code> trick? I suffered to merely create a regex which worked! Actually, until solving this problem, I did not even know the <code>-x<\/code> option.<\/p>\n<p>I once helped a friend to process a good number of files. He already spent more than two hours trying to do it with Java, and we solved it together in ten minutes with shell script. He then asked me how much he wanted to know shell script and asked me how to learn it.<\/p>\n<p>Well, little examples (like this one), as simple as they seem, taught me a lot. This is how I learned to script: trying to solve problems, knowing new commands and options in small batches. In the end, this is a valuable skill.<\/p>\n<p>So, I hope this little toying enrich your day, too. I certainly enriched mine \u2014 I&#8217;d like to think about it before spending three times more time with my Python script!<\/p>\n<p><small><em>This post is a translation of <a href=\"http:\/\/suspensao.blog.br\/descrenca\/trocando-figurinhas-sobre-o-terminal\/\">Trocando figurinhas sobre o terminal<\/a>.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my hobbies during this recent World Cup was to collect stickers. Actually, I\u2019ve built the sticker album because my son wanted it but I had fun, too, I guess. An important part of collecting stickers is to exchange the repeated ones. Through messages in WhatsApp groups, we report which repeated stickers we have &hellip; <a href=\"https:\/\/suspensao.blog.br\/disbelief\/exchanging-sticker-cards-with-the-terminal\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Exchanging World Cup&#8217;s sticker figures with the terminal&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[11,12,10,9],"tags":[15,19,17,14,18,16,13],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-grep","category-sed","category-shell-script","category-unix","tag-collectibles","tag-grep","tag-shell-script","tag-sticker-album","tag-terminal","tag-unix","tag-world-cup"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9Ru6q-K","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":4,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/posts\/46\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}