<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>csv &amp;mdash; Paul Sutton</title>
    <link>https://personaljournal.ca/paulsutton/tag:csv</link>
    <description>Personal Blog</description>
    <pubDate>Tue, 05 May 2026 18:11:27 +0000</pubDate>
    <item>
      <title>Bash scripting 14 - Generating data files</title>
      <link>https://personaljournal.ca/paulsutton/bash-scripting-14-generating-data-files</link>
      <description>&lt;![CDATA[Bash scripting 14 - Generating data files&#xA;&#xA;Issue 249, May 2025 of Linux Magazine, p52-55, had an article on using awk, and gnu plot to manipulate data.   In order to try and make use of this, and learn more, I am trying to figure out how to write a csv file. &#xA;&#xA;Write 10 lines to the first column should be in sequence e.g. 1-10 and the 2nd column should be random numbers.   This should be written to a file. &#xA;&#xA;So after some digging I managed to come up with this,  by modifying some existing solutions&#xA;&#xA;writecsv(){&#xA;    echo \&#34;$1\&#34;,\&#34;$2\&#34;   log.csv # write to file, use     to append&#xA;}&#xA;&#xA;sources of help&#xA;echo &#34;number&#34; &#34;data&#34; # for reference, not written to file &#xA;for ((i = 0 ; i &lt; 10 ; i++)); do&#xA;    echo $i, $RANDOM # echo to screen (stdout)&#xA;&#x9;echo $i, $RANDOM     log.csv # write to file&#xA;    #writecsv $(($i, $RANDOM % 10))&#xA;done&#xA;The screen output for me was&#xA;&#xA;number data&#xA;0, 17109&#xA;1, 2872&#xA;2, 22572&#xA;3, 7228&#xA;4, 27923&#xA;5, 6335&#xA;6, 20004&#xA;7, 32343&#xA;8, 10005&#xA;9, 21905&#xA;While the file output (log.csv) was similar but without the headings, which I just put in for my own reference,   the file does work as a csv as I can open in LibreOffice Calc. &#xA;&#xA;0, 17109&#xA;1, 2872&#xA;2, 22572&#xA;3, 7228&#xA;4, 27923&#xA;5, 6335&#xA;6, 20004&#xA;7, 32343&#xA;8, 10005&#xA;9, 21905&#xA;As the numbers are quite big,  a closer examination, promoted me to make the following modification&#xA;&#xA;echo $i, $((RANDOM % 20)) # echo to screen (stdout)&#xA;echo $i, $((RANDOM % 20))     log.csv # write to file&#xA;This fixes the 2nd column to produce a number between 1 and 20.&#xA;&#xA;number data&#xA;0, 17&#xA;1, 12&#xA;2, 12&#xA;3, 16&#xA;4, 2&#xA;5, 13&#xA;6, 10&#xA;7, 6&#xA;8, 16&#xA;9, 16&#xA;cat log.csv&#xA;0, 3&#xA;1, 17&#xA;2, 8&#xA;3, 16&#xA;4, 10&#xA;5, 18&#xA;6, 5&#xA;7, 14&#xA;8, 11&#xA;9, 11&#xA;&#xA;The script was called gencsv1.sh,  which as expected needed execute permissions &#xA;&#xA;chmod +x gencsv.sh&#xA;&#xA;References&#xA;&#xA;In order to help me I used the following, so giving credit to these sources as would be expected. &#xA;&#xA;https://stackoverflow.com/questions/40175868/how-to-create-csv-file-using-shell-script&#xA;https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash&#xA;https://linuxhandbook.com/bash-loops/&#xA;&#xA;Tags&#xA;&#xA;#Bash,#Bashscripting,#Files,#Random,#Data,#CSV,&#xA;&#xA;hr&#xD;&#xA;&#xD;&#xA;table&#xD;&#xA;thead&#xD;&#xA;trtda rel=&#34;me&#34; href=&#34;https://qoto.org/@zleap&#34;Mastodon/a/td&#xD;&#xA;tda href=&#34;https://wiki.ircnow.org/?n=Shelllabs.Intro&#34;ShellLabs/td&#xD;&#xA;tda href=&#34;https://joinmastodon.org/&#34;Join Mastodon/a/td/tr/thead/table&#xD;&#xA;center&#xD;&#xA;AI statement : b Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity.  Consent CANNOT be assumed, it has to be granted. /b&#xD;&#xA;/center&#xD;&#xA;&#xD;&#xA;a href=&#34;https://liberapay.com/PaulSutton/donate&#34;img alt=&#34;Donate using Liberapay&#34; src=&#34;https://liberapay.com/assets/widgets/donate.svg&#34;/a&#xD;&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Bash scripting 14 – Generating data files</p>

<p>Issue 249, May 2025 of Linux Magazine, p52-55, had an article on using awk, and gnu plot to manipulate data.   In order to try and make use of this, and learn more, I am trying to figure out how to write a csv file.</p>

<p>Write 10 lines to the first column should be in sequence e.g. 1-10 and the 2nd column should be random numbers.   This should be written to a file.</p>

<p>So after some digging I managed to come up with this,  by modifying some existing solutions</p>

<pre><code class="language-bash">write_csv(){
    echo \&#34;$1\&#34;,\&#34;$2\&#34; &gt; log.csv # write to file, use &gt;&gt; to append
}

#sources of help
echo &#34;number&#34; &#34;data&#34; # for reference, not written to file 
for ((i = 0 ; i &lt; 10 ; i++)); do
    echo $i, $RANDOM # echo to screen (stdout)
	echo $i, $RANDOM &gt;&gt; log.csv # write to file
    #write_csv $(($i, $RANDOM % 10))
done
</code></pre>

<p>The screen output for me was</p>

<pre><code>number data
0, 17109
1, 2872
2, 22572
3, 7228
4, 27923
5, 6335
6, 20004
7, 32343
8, 10005
9, 21905
</code></pre>

<p>While the file output (log.csv) was similar but without the headings, which I just put in for my own reference,   the file does work as a csv as I can open in LibreOffice Calc.</p>

<pre><code>0, 17109
1, 2872
2, 22572
3, 7228
4, 27923
5, 6335
6, 20004
7, 32343
8, 10005
9, 21905
</code></pre>

<p>As the numbers are quite big,  a closer examination, promoted me to make the following modification</p>

<pre><code class="language-bash">echo $i, $((RANDOM % 20)) # echo to screen (stdout)
echo $i, $((RANDOM % 20)) &gt;&gt; log.csv # write to file
</code></pre>

<p>This fixes the 2nd column to produce a number between 1 and 20.</p>

<pre><code>number data
0, 17
1, 12
2, 12
3, 16
4, 2
5, 13
6, 10
7, 6
8, 16
9, 16
cat log.csv
0, 3
1, 17
2, 8
3, 16
4, 10
5, 18
6, 5
7, 14
8, 11
9, 11

</code></pre>

<p>The script was called gencsv1.sh,  which as expected needed execute permissions</p>

<pre><code>chmod +x gencsv.sh
</code></pre>

<p><strong>References</strong></p>

<p>In order to help me I used the following, so giving credit to these sources as would be expected.</p>
<ul><li><a href="https://stackoverflow.com/questions/40175868/how-to-create-csv-file-using-shell-script" rel="nofollow">https://stackoverflow.com/questions/40175868/how-to-create-csv-file-using-shell-script</a></li>
<li><a href="https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash" rel="nofollow">https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash</a></li>
<li><a href="https://linuxhandbook.com/bash-loops/" rel="nofollow">https://linuxhandbook.com/bash-loops/</a></li></ul>

<p><strong>Tags</strong></p>

<p><a href="/paulsutton/tag:Bash" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Bash</span></a>,<a href="/paulsutton/tag:Bashscripting" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Bashscripting</span></a>,<a href="/paulsutton/tag:Files" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Files</span></a>,<a href="/paulsutton/tag:Random" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Random</span></a>,<a href="/paulsutton/tag:Data" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Data</span></a>,<a href="/paulsutton/tag:CSV" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">CSV</span></a>,</p>

<hr>

<p><table>
<thead>
<tr><td><a href="https://qoto.org/@zleap" rel="nofollow">Mastodon</a></td>
<td><a href="https://wiki.ircnow.org/?n=Shelllabs.Intro" rel="nofollow">ShellLabs</td>
<td><a href="https://joinmastodon.org/" rel="nofollow">Join Mastodon</a></td></tr></thead></table>

AI statement : <b> Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity.  Consent CANNOT be assumed, it has to be granted. </b>
</p>

<p><a href="https://liberapay.com/PaulSutton/donate" rel="nofollow"><img alt="Donate using Liberapay" src="https://liberapay.com/assets/widgets/donate.svg"></a></p>
]]></content:encoded>
      <guid>https://personaljournal.ca/paulsutton/bash-scripting-14-generating-data-files</guid>
      <pubDate>Wed, 16 Apr 2025 06:30:00 +0000</pubDate>
    </item>
  </channel>
</rss>