<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kurukurin</title>
    <description>The latest articles on DEV Community by kurukurin (@kurukurin).</description>
    <link>https://dev.to/kurukurin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2658717%2F69cc5f3c-73b2-468e-94bb-a3a003046e7e.jpeg</url>
      <title>DEV Community: kurukurin</title>
      <link>https://dev.to/kurukurin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kurukurin"/>
    <language>en</language>
    <item>
      <title>Ergonomic Pyhon Text Piping Solution for Linux Shell with pypyp and uv</title>
      <dc:creator>kurukurin</dc:creator>
      <pubDate>Sun, 05 Jan 2025 07:28:37 +0000</pubDate>
      <link>https://dev.to/kurukurin/ergonomic-pyhon-text-piping-solution-for-linux-shell-with-pypyp-and-uv-36fh</link>
      <guid>https://dev.to/kurukurin/ergonomic-pyhon-text-piping-solution-for-linux-shell-with-pypyp-and-uv-36fh</guid>
      <description>&lt;h2&gt;
  
  
  Abstract
&lt;/h2&gt;

&lt;p&gt;This short blog post is an introduction about a linux text piping solution with &lt;a href="https://github.com/hauntsaninja/pyp" rel="noopener noreferrer"&gt;pypyp&lt;/a&gt; and &lt;a href="https://docs.astral.sh/uv/" rel="noopener noreferrer"&gt;uv&lt;/a&gt;, it can easily reuse all your knowledge and packages about python without learning &lt;code&gt;awk&lt;/code&gt;. We focus on telling the reader why choosing it instead of how to use it. If you want to learn more about the usage, visit &lt;a href="https://github.com/hauntsaninja/pyp" rel="noopener noreferrer"&gt;pypyp's homepage&lt;/a&gt; and &lt;a href="https://docs.astral.sh/uv/" rel="noopener noreferrer"&gt;uv's homepage&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I won't use &lt;code&gt;awk&lt;/code&gt; ?
&lt;/h2&gt;

&lt;p&gt;When writing linux shell scripts or commands, &lt;code&gt;awk&lt;/code&gt; , &lt;code&gt;sed&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt; are powerfull tools for working with texts: You can use &lt;code&gt;grep&lt;/code&gt; to find something like &lt;code&gt;ls | grep myname&lt;/code&gt;, use &lt;code&gt;sed&lt;/code&gt; to replace something and use &lt;code&gt;awk&lt;/code&gt; as a turing complete programming languages to deal with more sophisticated cases.&lt;br&gt;
&lt;code&gt;grep&lt;/code&gt; and &lt;code&gt;sed&lt;/code&gt; are fine. They do one thing and do it very well. But &lt;code&gt;awk&lt;/code&gt; is not. As we know, &lt;code&gt;awk&lt;/code&gt; is a programming language for text, and it takes more time to learn how to use it comparing to &lt;code&gt;grep&lt;/code&gt; and &lt;code&gt;sed&lt;/code&gt;. That's the problem, &lt;code&gt;awk&lt;/code&gt; is a good text processing &lt;strong&gt;tool&lt;/strong&gt; but not a good programming language.&lt;br&gt;
Comparing to Python, Ruby and Perl, &lt;code&gt;awk&lt;/code&gt; is not a general purpose programming language, so the 99% usage of &lt;code&gt;awk&lt;/code&gt; is only processing texts in linux shell, and the convenience of that is not worth your time and cognitive loading for learning a new programming language, especially when you're not majoring in shell scripting.&lt;br&gt;
So, life is short, why learning another programming language if you can use   the one that you have already learnt?&lt;/p&gt;
&lt;h2&gt;
  
  
  Why I choose pypyp?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/hauntsaninja/pyp" rel="noopener noreferrer"&gt;pypyp&lt;/a&gt; is a solution. It's a simple (less than 800 lines of code) python script than could help you replace &lt;code&gt;awk&lt;/code&gt; , &lt;code&gt;sed&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt; with a single command &lt;code&gt;pyp&lt;/code&gt;, with all your knowledge about python. Here's a quick example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;uname&lt;/span&gt; | pyp &lt;span class="s1"&gt;'x.lower()'&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; | uvx pypyp &lt;span class="s1"&gt;'re.match(r"\S+.c",x)'&lt;/span&gt;   &lt;span class="c"&gt;# use python regex&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pypyp&lt;/code&gt; solves many simple but important problems about &lt;code&gt;python -c&lt;/code&gt;, it reads &lt;code&gt;stdin&lt;/code&gt; to &lt;code&gt;lines&lt;/code&gt; variable and split &lt;code&gt;lines&lt;/code&gt; to &lt;code&gt;x&lt;/code&gt; variable, it also print the last expression automaticlly. Meanwhile it imports some comman packages to make python as easy to use as a text processing language for linux shell as perl and &lt;code&gt;awk&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I also use uv?
&lt;/h2&gt;

&lt;p&gt;uv is like the &lt;code&gt;cargo&lt;/code&gt; or &lt;code&gt;npm&lt;/code&gt; for python. Using &lt;code&gt;pypyp&lt;/code&gt; with &lt;code&gt;uvx&lt;/code&gt; (works like &lt;code&gt;npx&lt;/code&gt; or &lt;code&gt;pipx&lt;/code&gt;) is really easy especially your need third party packages for &lt;code&gt;pypyp&lt;/code&gt;. For example, I want to use &lt;code&gt;numpy&lt;/code&gt; with &lt;code&gt;pypyp&lt;/code&gt;, I can simply use &lt;code&gt;uvx --with numpy&lt;/code&gt; to add &lt;code&gt;numpy&lt;/code&gt; package and use &lt;code&gt;pyp&lt;/code&gt; to automaticlly import it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;uvx &lt;span class="nt"&gt;--with&lt;/span&gt; numpy pypyp &lt;span class="s1"&gt;'numpy.random.randint(100)'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;uv also make installing &lt;code&gt;pypyp&lt;/code&gt; easier. Once uv is installed, you can dirctly run &lt;code&gt;uvx pypyp&lt;/code&gt; and uvx will download and run it for you.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I found that &lt;code&gt;uvx pypyp&lt;/code&gt; is a good alternative for &lt;code&gt;awk&lt;/code&gt;, it can reuse all your knowledge about python, without adding more burden for you. But we should also notice that it is not a popular solution for now, and it's better not to share your commands or scripts with others for compatibility.&lt;/p&gt;

</description>
      <category>python</category>
      <category>linux</category>
      <category>shell</category>
      <category>bash</category>
    </item>
  </channel>
</rss>
