Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

Bash

AWK timestamp each line

<cmd> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
https://snipt.net/embed/3c95a0173f04f01070d3df97f8aa825b/
https://snipt.net/raw/3c95a0173f04f01070d3df97f8aa825b/
3c95a0173f04f01070d3df97f8aa825b
bash
Bash
1
2016-12-10T16:01:59
True
False
False
Mar 25, 2016 at 06:05 PM
/api/public/snipt/144950/
awk-timestamp-each-line
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>&lt;cmd&gt; <span class="p">|</span> awk <span class="s1">&#39;{ print strftime(&quot;%Y-%m-%d %H:%M:%S&quot;), $0; fflush(); }&#39;</span> </span></pre></div> </td></tr></table>
awk, bash, console, timestamp

Markdown

Transform file list and more sed tricks

This command will print the filename and save them filtering only the part before the underscore.

ls| sed 's/_/ /g' | awk 'BEGIN {} $1 ~ /[0-9]/ {print $1}' > ../subcodes.txt
https://snipt.net/embed/d1cc07b2865af9f79b029efd2bb24313/
https://snipt.net/raw/d1cc07b2865af9f79b029efd2bb24313/
d1cc07b2865af9f79b029efd2bb24313
markdown
Markdown
3
2016-12-06T14:15:16
True
False
False
Mar 25, 2014 at 01:28 PM
/api/public/snipt/124715/
transform-file-list-and-more-sed-tricks
<p>This command will print the filename and save them filtering only the part before the underscore.</p> <pre><code>ls| sed 's/_/ /g' | awk 'BEGIN {} $1 ~ /[0-9]/ {print $1}' &gt; ../subcodes.txt </code></pre>
awk, sed

Text only

[awk] ダブルクオテーションで囲まれたカンマを含む値のあるcsvを扱う

awk 'BEGIN { FPAT = "([^, ]+)|(\"[^\"]+\")" } { sum+=$3 } END { print sum }' file.txt
https://snipt.net/embed/17de1357b9d18d1677a139270ffdcd2e/
https://snipt.net/raw/17de1357b9d18d1677a139270ffdcd2e/
17de1357b9d18d1677a139270ffdcd2e
text
Text only
2
2016-12-07T22:18:16
True
False
False
/api/public/snipt/111274/
awk-csv
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>awk &#39;BEGIN { FPAT = &quot;([^, ]+)|(\&quot;[^\&quot;]+\&quot;)&quot; } { sum+=$3 } END { print sum }&#39; file.txt </span></pre></div> </td></tr></table>
awk

Bash

kill process that contains string but not another string

1
2
3
4
5
# command line:
$ pgrep -lf "spork" | grep -v "tmux" | awk '{print $1}' | xargs kill -9

# via yaml or environment hostile to special characters (e.g., tmuxinator config)
$ kill -9 $(pgrep -lf "rails s" | grep -v "tmux" | cut -d " " -f1); rails s
https://snipt.net/embed/f78fdc50c4bf053db181c85adfb124ea/
https://snipt.net/raw/f78fdc50c4bf053db181c85adfb124ea/
f78fdc50c4bf053db181c85adfb124ea
bash
Bash
6
2016-12-04T05:28:12
True
False
False
Apr 11, 2013 at 12:51 AM
/api/public/snipt/56301/
kill-process-that-contains-string-but-not-another-string
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1"># command line:</span> </span><span id="L-2"><a name="L-2"></a>$ pgrep -lf <span class="s2">&quot;spork&quot;</span> <span class="p">|</span> grep -v <span class="s2">&quot;tmux&quot;</span> <span class="p">|</span> awk <span class="s1">&#39;{print $1}&#39;</span> <span class="p">|</span> xargs <span class="nb">kill</span> -9 </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1"># via yaml or environment hostile to special characters (e.g., tmuxinator config)</span> </span><span id="L-5"><a name="L-5"></a>$ <span class="nb">kill</span> -9 <span class="k">$(</span>pgrep -lf <span class="s2">&quot;rails s&quot;</span> <span class="p">|</span> grep -v <span class="s2">&quot;tmux&quot;</span> <span class="p">|</span> cut -d <span class="s2">&quot; &quot;</span> -f1<span class="k">)</span><span class="p">;</span> rails s </span></pre></div> </td></tr></table>
awk, cut, grep, kill, linux, pgrep, process, rails, server, spork, tmux, tmuxinator, ubuntu, xargs

Text only

archive svn modified files

1
2
3
4
5
6
7
8
#archive all svn modified filed
svn st | grep ^M | awk '{print $2}' | xargs tar czf changed.tar.gz

#list contents of the archive
tar -tf changed.tar.gz

#for an entire repository
svn export URL PATH ; tar czf exported_copy.tgz PATH
https://snipt.net/embed/9e9353251d821ca3391aa73acb579fc0/
https://snipt.net/raw/9e9353251d821ca3391aa73acb579fc0/
9e9353251d821ca3391aa73acb579fc0
text
Text only
8
2016-12-10T07:41:05
True
False
False
/api/public/snipt/46533/
archive-svn-modified-files
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>#archive all svn modified filed </span><span id="L-2"><a name="L-2"></a>svn st | grep ^M | awk &#39;{print $2}&#39; | xargs tar czf changed.tar.gz </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a>#list contents of the archive </span><span id="L-5"><a name="L-5"></a>tar -tf changed.tar.gz </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a>#for an entire repository </span><span id="L-8"><a name="L-8"></a>svn export URL PATH ; tar czf exported_copy.tgz PATH </span></pre></div> </td></tr></table>
awk, grep, svn

Bash

Search zombie process in linux with awk or without awk

1
2
3
4
5
#more effective, using awk
ps aux | awk '{ print $8 " " $2 }' | grep -w Z

#less effective, without awk
ps -el | grep 'Z'
https://snipt.net/embed/3bc0b112edf34393e223f18bd9b6d361/
https://snipt.net/raw/3bc0b112edf34393e223f18bd9b6d361/
3bc0b112edf34393e223f18bd9b6d361
bash
Bash
5
2016-12-10T01:23:02
True
False
False
/api/public/snipt/45495/
search-zombie-process-in-linux-with-awk-or-without-awk
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">#more effective, using awk</span> </span><span id="L-2"><a name="L-2"></a>ps aux <span class="p">|</span> awk <span class="s1">&#39;{ print $8 &quot; &quot; $2 }&#39;</span> <span class="p">|</span> grep -w Z </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">#less effective, without awk</span> </span><span id="L-5"><a name="L-5"></a>ps -el <span class="p">|</span> grep <span class="s1">&#39;Z&#39;</span> </span></pre></div> </td></tr></table>
"search zombie process", awk, bash, linux, process, sh, zombie

Bash

Renaming files via csv and bash

1
2
3
4
5
// Dry run
awk -F, '{print("mv \"" $1 "\" \"" $2 "\"")}' rename-these11.csv

// For real this time
awk -F, '{print("mv \"" $1 "\" \"" $2 "\"")}' rename-these11.csv | /bin/bash
https://snipt.net/embed/050d70bf81ca76d62928837df75af630/
https://snipt.net/raw/050d70bf81ca76d62928837df75af630/
050d70bf81ca76d62928837df75af630
bash
Bash
6
2016-12-09T17:46:23
True
False
False
/api/public/snipt/41973/
renaming-files-via-csv-and-bash
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>// Dry run </span><span id="L-2"><a name="L-2"></a>awk -F, <span class="s1">&#39;{print(&quot;mv \&quot;&quot; $1 &quot;\&quot; \&quot;&quot; $2 &quot;\&quot;&quot;)}&#39;</span> rename-these11.csv </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a>// For real this <span class="nb">time</span> </span><span id="L-5"><a name="L-5"></a>awk -F, <span class="s1">&#39;{print(&quot;mv \&quot;&quot; $1 &quot;\&quot; \&quot;&quot; $2 &quot;\&quot;&quot;)}&#39;</span> rename-these11.csv <span class="p">|</span> /bin/bash </span></pre></div> </td></tr></table>
awk, bash, linux, mac, unix

Awk

Remove html tags

awk '{gsub(/<[^>]+>/,"");print}' $1
https://snipt.net/embed/662565e2dae18f1f2a0e56200ac35ca7/
https://snipt.net/raw/662565e2dae18f1f2a0e56200ac35ca7/
662565e2dae18f1f2a0e56200ac35ca7
awk
Awk
1
2016-12-06T13:57:17
True
False
False
/api/public/snipt/39700/
remove-html-tags
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">awk</span> <span class="s1">&#39;{gsub(/&lt;[^&gt;]+&gt;/,&quot;&quot;);print}&#39;</span> <span class="o">$</span><span class="mi">1</span> </span></pre></div> </td></tr></table>
awk, html
Copyrighted, illegal, or inappropriate content? Email [email protected].