<?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: Vetle Leinonen-Roeim</title>
    <description>The latest articles on DEV Community by Vetle Leinonen-Roeim (@vetler).</description>
    <link>https://dev.to/vetler</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%2F19285%2F7d346040-fee1-40a9-9a29-fa0249edb4bd.jpg</url>
      <title>DEV Community: Vetle Leinonen-Roeim</title>
      <link>https://dev.to/vetler</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vetler"/>
    <language>en</language>
    <item>
      <title>Command Line Warrior - filter out non-existing URLs</title>
      <dc:creator>Vetle Leinonen-Roeim</dc:creator>
      <pubDate>Mon, 21 Aug 2023 13:20:00 +0000</pubDate>
      <link>https://dev.to/vetler/command-line-warrior-filter-out-non-existing-urls-46nf</link>
      <guid>https://dev.to/vetler/command-line-warrior-filter-out-non-existing-urls-46nf</guid>
      <description>&lt;h1&gt;
  
  
  Problem description
&lt;/h1&gt;

&lt;p&gt;Given a file of URLs, how can we filter out any that gives us HTTP 404 Not Found and write the URLs that exist to a new file?&lt;/p&gt;

&lt;h1&gt;
  
  
  TL;DR
&lt;/h1&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat urls.txt \
  | xargs -I{} sh -c 'curl -sIL {} -w "%{http_code}" -o /dev/null \
    | grep -q -v 404 &amp;amp;&amp;amp; echo {}' &amp;gt; ok_urls.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Explanation
&lt;/h1&gt;

&lt;p&gt;First, we pipe the list of URLs to &lt;code&gt;xargs&lt;/code&gt; using &lt;code&gt;cat&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat urls.txt | xargs ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Using &lt;code&gt;xargs&lt;/code&gt; we read the input from &lt;code&gt;cat&lt;/code&gt; and execute a shell command for each line. The &lt;code&gt;-I{}&lt;/code&gt; tells &lt;code&gt;xargs&lt;/code&gt; that we want to replace the string &lt;code&gt;{}&lt;/code&gt; with the input (in this case a URL).&lt;/p&gt;

&lt;p&gt;Since we need to output the URL we got as an input, we will actually use this twice: once first when checking the URL, second when outputting the URL that was valid.&lt;/p&gt;

&lt;p&gt;To run multiple commands for each line, we tell &lt;code&gt;xargs&lt;/code&gt; to run a shell with another command, as specified with &lt;code&gt;-c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the next part of the script, we first use &lt;code&gt;curl&lt;/code&gt; to access the URL, telling it to be silent and don't give us output we don't need with &lt;code&gt;-s&lt;/code&gt;, only give us the header &lt;code&gt;-I&lt;/code&gt; and follow any redirects &lt;code&gt;-L&lt;/code&gt;. To get the status code only, we use &lt;code&gt;-w "%{http_code}"&lt;/code&gt;. This flag can be used to tailor the output from curl. &lt;code&gt;-o /dev/null&lt;/code&gt; sends any other output to somewhere it can be discarded.&lt;/p&gt;

&lt;p&gt;To filter out 404 Not Found we can use &lt;code&gt;grep -v&lt;/code&gt; which will match lines that do &lt;em&gt;not&lt;/em&gt; contain 404, and &lt;code&gt;-q&lt;/code&gt; makes grep be quiet. This way we can test only on the exit value of &lt;code&gt;grep&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you combine two commands with &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, the last one will only be run if the previous command was successful - so by putting &lt;code&gt;&amp;amp;&amp;amp; echo {}&lt;/code&gt; after grep, the URL will only be printed if &lt;code&gt;grep&lt;/code&gt; was successful. Remember that &lt;code&gt;{}&lt;/code&gt; is replaced with the URL by &lt;code&gt;xargs&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Finally, we send the list of URLs to a new file, and we're done!&lt;/p&gt;

&lt;p&gt;Happy hacking,&lt;br&gt;
Vetle&lt;/p&gt;

</description>
      <category>commandline</category>
      <category>linux</category>
      <category>bash</category>
      <category>scripting</category>
    </item>
    <item>
      <title>Am I the only one who thinks we've made software development way more complicated than it has to be?</title>
      <dc:creator>Vetle Leinonen-Roeim</dc:creator>
      <pubDate>Thu, 05 Mar 2020 06:18:24 +0000</pubDate>
      <link>https://dev.to/vetler/am-i-the-only-one-who-thinks-we-ve-made-software-development-way-more-complicated-than-it-has-to-be-1l8</link>
      <guid>https://dev.to/vetler/am-i-the-only-one-who-thinks-we-ve-made-software-development-way-more-complicated-than-it-has-to-be-1l8</guid>
      <description></description>
      <category>discuss</category>
    </item>
    <item>
      <title>How many programming language years old are you?</title>
      <dc:creator>Vetle Leinonen-Roeim</dc:creator>
      <pubDate>Fri, 30 Aug 2019 13:43:06 +0000</pubDate>
      <link>https://dev.to/vetler/how-many-programming-language-years-old-are-you-287m</link>
      <guid>https://dev.to/vetler/how-many-programming-language-years-old-are-you-287m</guid>
      <description>&lt;p&gt;I'm SIMULA years old.&lt;/p&gt;

</description>
      <category>jokes</category>
      <category>watercooler</category>
      <category>whysoserious</category>
    </item>
    <item>
      <title>User Inyerface</title>
      <dc:creator>Vetle Leinonen-Roeim</dc:creator>
      <pubDate>Thu, 04 Jul 2019 08:25:11 +0000</pubDate>
      <link>https://dev.to/vetler/user-inyerface-gpm</link>
      <guid>https://dev.to/vetler/user-inyerface-gpm</guid>
      <description>&lt;p&gt;&lt;a href="https://userinyerface.com"&gt;https://userinyerface.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jokes</category>
    </item>
  </channel>
</rss>
