snipt

Ctrl+h for KB shortcuts
Notice: Snipt is moving away from free accounts on May 1st, 2014. Read more about the transition here.
#28393

C++

Split a comma-separated string into a whitespace-trimmed sequence of non-empty tokens

1
2
3
4
5
6
std::string s; // Source data
std::vector<std::string> tokens;
boost::algorithm::split(tokens, s, boost::algorithm::is_any_of(","));
std::for_each(tokens.begin(), tokens.end(),
              boost::bind(boost::algorithm::trim<std::string>, _1, std::locale()));
tokens.erase(std::remove(tokens.begin(), tokens.end(), ""), tokens.end());
https://snipt.net/embed/f50cf584128efbca9194cd83643608a0/
https://snipt.net/raw/f50cf584128efbca9194cd83643608a0/
f50cf584128efbca9194cd83643608a0
cpp
C++
7
2014-04-23T02:58:30
True
False
/api/public/snipt/28393/
split-a-comma-separated-string-into-a-whitespace-trimmed-sequence-of-non-empty-tokens
<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></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">s</span><span class="p">;</span> <span class="c1">// Source data</span> </span><span id="L-2"><a name="L-2"></a><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span> <span class="n">tokens</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="n">boost</span><span class="o">::</span><span class="n">algorithm</span><span class="o">::</span><span class="n">split</span><span class="p">(</span><span class="n">tokens</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">algorithm</span><span class="o">::</span><span class="n">is_any_of</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">));</span> </span><span id="L-4"><a name="L-4"></a><span class="n">std</span><span class="o">::</span><span class="n">for_each</span><span class="p">(</span><span class="n">tokens</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">tokens</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> </span><span id="L-5"><a name="L-5"></a> <span class="n">boost</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">algorithm</span><span class="o">::</span><span class="n">trim</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">_1</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">locale</span><span class="p">()));</span> </span><span id="L-6"><a name="L-6"></a><span class="n">tokens</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">remove</span><span class="p">(</span><span class="n">tokens</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">tokens</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="s">&quot;&quot;</span><span class="p">),</span> <span class="n">tokens</span><span class="p">.</span><span class="n">end</span><span class="p">());</span> </span></pre></div> </td></tr></table>
csv, parse, string
#26062

Bash

Use awk to strip column headings from the topmost line of a whitespace-delimited CSV file

1
2
3
4
5
6
7
#!/usr/bin/awk -f
(NR==1) {
    for(i=1; i<=NF; i+=1) {
        sub(", *$", "", $i);
        printf("%2d  %s\n", i, $i);
    }
}
https://snipt.net/embed/5cac0680ba2ef33232ea165b56193226/
https://snipt.net/raw/5cac0680ba2ef33232ea165b56193226/
5cac0680ba2ef33232ea165b56193226
bash
Bash
7
2014-04-16T21:17:24
True
False
/api/public/snipt/26062/
a-bash-script-that-uses-awk-to-strip-column-headings-from-the-topmost-line-of-a-whitespace-delimited-csv-file
<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></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c">#!/usr/bin/awk -f</span> </span><span id="L-2"><a name="L-2"></a><span class="o">(</span><span class="nv">NR</span><span class="o">==</span>1<span class="o">)</span> <span class="o">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">for</span><span class="o">(</span><span class="nv">i</span><span class="o">=</span>1<span class="p">;</span> i&lt;<span class="o">=</span>NF<span class="p">;</span> i+<span class="o">=</span>1<span class="o">)</span> <span class="o">{</span> </span><span id="L-4"><a name="L-4"></a> sub<span class="o">(</span><span class="s2">&quot;, *$&quot;</span>, <span class="s2">&quot;&quot;</span>, <span class="nv">$i</span><span class="o">)</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a> <span class="nb">printf</span><span class="o">(</span><span class="s2">&quot;%2d %s\n&quot;</span>, i, <span class="nv">$i</span><span class="o">)</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a> <span class="o">}</span> </span><span id="L-7"><a name="L-7"></a><span class="o">}</span> </span></pre></div> </td></tr></table>
awk, csv
#26061

Bash

An alias that can strip column headings from the topmost line of a whitespace-delimited CSV file

alias colnames='awk "(NR==1) {for(i=1;i<=NF;i+=1) {sub(\", \*$\", \"\", \$i); printf(\"%2d  %s\n\", i, \$i)}}"'
https://snipt.net/embed/1d8df7d9d01420806051ad7af7923b44/
https://snipt.net/raw/1d8df7d9d01420806051ad7af7923b44/
1d8df7d9d01420806051ad7af7923b44
bash
Bash
1
2014-04-23T04:05:47
True
False
/api/public/snipt/26061/
an-alias-that-can-strip-column-headings-from-the-topmost-line-of-a-whitespace-delimited-csv-file
<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 id="L-1"><a name="L-1"></a><span class="nb">alias </span><span class="nv">colnames</span><span class="o">=</span><span class="s1">&#39;awk &quot;(NR==1) {for(i=1;i&lt;=NF;i+=1) {sub(\&quot;, \*$\&quot;, \&quot;\&quot;, \$i); printf(\&quot;%2d %s\n\&quot;, i, \$i)}}&quot;&#39;</span> </span></pre></div> </td></tr></table>
awk, csv