<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>btaz &#187; Unix/Linux</title>
	<atom:link href="http://www.btaz.com/category/unixlinux/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.btaz.com</link>
	<description>Promoting the art of coding</description>
	<lastBuildDate>Thu, 17 Mar 2011 06:41:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Unix/Linux Sort Multiple Columns, Tab Delimited and Reverse Sort Order</title>
		<link>http://www.btaz.com/unixlinux/unixlinux-sort-multiple-columns-tab-delimited-and-reverse-sort-order/</link>
		<comments>http://www.btaz.com/unixlinux/unixlinux-sort-multiple-columns-tab-delimited-and-reverse-sort-order/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 15:57:49 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Unix/Linux]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[multiple columns]]></category>
		<category><![CDATA[reverse]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[tab delimited]]></category>
		<category><![CDATA[tab separated]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.btaz.com/?p=179</guid>
		<description><![CDATA[Sorting a tab delimited file using the Unix sort command is easy once you which parameters to use. An advanced file sort can get difficult if it has multiple columns, uses tab characters as the column separator, you want to reverse the sort order on some columns, and where you want the columns sorted in [...]


Related posts:<ol><li><a href='http://www.btaz.com/unixlinux/how-to-automatically-provide-an-answer-to-unix-commands/' rel='bookmark' title='Permanent Link: How to automatically provide an answer to Unix commands'>How to automatically provide an answer to Unix commands</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Sorting a tab delimited file using the Unix sort command is easy once you which parameters to use. An advanced file sort can get difficult if it has multiple columns, uses tab characters as the column separator, you want to reverse the sort order on some columns, and where you want the columns sorted in non-sequential order.</p>
<p>Assume that we have the following file where each column is separated by a [TAB] character:</p>
<pre>Group-ID   Category-ID   Text        Frequency
----------------------------------------------
200        1000          oranges     10
200        900           bananas     5
200        1000          pears       8
200        1000          lemons      10
200        900           figs        4
190        700           grapes      17</pre>
<p>I&#8217;d like to have this file sorted by these columns and in this specific order (note that column 4 is sorted before column 3 and that column 4 is sorted in reverse order):</p>
<ul>
<li>Group ID (integer)</li>
<li>Category ID (integer)</li>
<li>Frequency &#8220;sorted in reverse order&#8221; (integer)</li>
<li>Text (alpha-numeric)</li>
</ul>
<p>This should sort the file into this format:</p>
<pre>Group-ID   Category-ID   Text        Frequency
----------------------------------------------
190        700           grapes      17
200        900           bananas     5
200        900           figs        4
200        1000          lemons      10
200        1000          oranges     10
200        1000          pears       8</pre>
<p>The quick answer is that these sort arguments would solve the problem:</p>
<pre>sort -t $'\t' -k 1n,1 -k 2n,2 -k4rn,4 -k3,3 &lt;my-file&gt;</pre>
<p>A description of what it all means please read on. The first thing we need to do is to tell sort to use TAB as a column separator (column separated or delimited) which we can do using:</p>
<pre>sort -t $'\t' &lt;my-file&gt;</pre>
<p>If our input file was comma separated we could have used:</p>
<pre>sort -t "," &lt;my-file&gt;</pre>
<p>The next step is define that we want the file sorted by columns 1, 2, 4 and 3 and in this particular order. The key argument &#8220;-k&#8221; allows us to do this. The tricky part is that you have to define the column index twice to limit the sort to any given column, e.g. like this &#8220;-k 1,1&#8243;. If you only specify it once like this &#8220;-k 1&#8243; you&#8217;re telling Unix &#8220;sort&#8221; to sort the file from column 1 and until the end of the line which is not what we want. If you want to sort column 1 and 2 together you&#8217;d use &#8220;-k 1,2&#8243;.  To tell sort to sort multiple columns we have to define the key argument &#8220;-k&#8221; multiple times. The sort arguments required to sort our file in column order 1, 2, 4 and 3 will therefore look like this:</p>
<pre>sort -t $'\t' -k 1,1 -k 2,2 -k 4,4 -k 3,3 &lt;my-file&gt;</pre>
<p>We however want the 4th column sorted in reverse order which we can instruct sort to do by changing the argument from &#8220;-k 4,4&#8243; to &#8220;-k 4r,4&#8243;. The &#8220;r&#8221; option reverses the sort order that column only. There&#8217;s only one problem left to solve and that is that sort by default will interpret numbers as text and will sort e.g.  the number 10 ahead of 2. We solve this by adding the &#8220;n&#8221; option to tell &#8220;sort&#8221; to sort a column using its numerical values e.g. &#8220;-k 1n,1&#8243;. Note that the &#8220;n&#8221; option is only attached to the first number to the left of the comma. Since the 4th column is sorted in both reversed order and using numerical values we can combine the options like this &#8220;-k 4rn,4&#8243;</p>
<p>So by adding all of these options together with end up with:</p>
<pre>sort -t $'\t' -k 1n,1 -k 2n,2 -k 4rn,4 -k 3,3 &lt;my-file&gt;</pre>
<p>I hope someone will find this useful. I tested this solution on both Linux and OS X. The documentation for the Unix sort command can be found using your man command &#8220;man sort&#8221; and &#8220;info sort&#8221;.</p>


<p>Related posts:<ol><li><a href='http://www.btaz.com/unixlinux/how-to-automatically-provide-an-answer-to-unix-commands/' rel='bookmark' title='Permanent Link: How to automatically provide an answer to Unix commands'>How to automatically provide an answer to Unix commands</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.btaz.com/unixlinux/unixlinux-sort-multiple-columns-tab-delimited-and-reverse-sort-order/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ls full path</title>
		<link>http://www.btaz.com/unixlinux/ls-full-path/</link>
		<comments>http://www.btaz.com/unixlinux/ls-full-path/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 17:01:58 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Unix/Linux]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[full]]></category>
		<category><![CDATA[ls]]></category>
		<category><![CDATA[path]]></category>

		<guid isPermaLink="false">http://www.btaz.com/?p=168</guid>
		<description><![CDATA[How do you get the Unix command ls to show you the full path? Unfortunately there&#8217;s no argument for ls that will do this directly. However this will work fine and give you what you want. ls -d $PWD/* or ls -ld $PWD/* No related posts.


No related posts.]]></description>
			<content:encoded><![CDATA[<p>How do you get the Unix command ls to show you the full path? Unfortunately there&#8217;s no argument for ls that will do this directly.<br />
However this will work fine and give you what you want.</p>
<p>ls -d $PWD/*</p>
<p>or</p>
<p>ls -ld $PWD/*</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.btaz.com/unixlinux/ls-full-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to control or throttle SCP file transfer speed</title>
		<link>http://www.btaz.com/unixlinux/how-to-control-or-throttle-scp-file-transfer-speed/</link>
		<comments>http://www.btaz.com/unixlinux/how-to-control-or-throttle-scp-file-transfer-speed/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 18:43:13 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Unix/Linux]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[exceed]]></category>
		<category><![CDATA[file transer]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MBit/s]]></category>
		<category><![CDATA[SCP]]></category>
		<category><![CDATA[throttle]]></category>

		<guid isPermaLink="false">http://www.btaz.com/?p=94</guid>
		<description><![CDATA[I wanted to control how fast SCP copy data between two Linux servers. On a Linux system this can easily be achieved using the &#8220;SCP -l&#8221; switch. In my case I had to make sure to not exceed 0.5 Mbit/s Since the &#8220;-l&#8221; parameter uses Kbit/s the correct value in my case is 500 scp [...]


Related posts:<ol><li><a href='http://www.btaz.com/unixlinux/how-to-automatically-provide-an-answer-to-unix-commands/' rel='bookmark' title='Permanent Link: How to automatically provide an answer to Unix commands'>How to automatically provide an answer to Unix commands</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I wanted to control how fast SCP copy data between two Linux servers. On a Linux system this can easily be achieved using the &#8220;SCP -l&#8221; switch. In my case I had to make sure to not exceed 0.5 Mbit/s</p>
<p>Since the &#8220;-l&#8221; parameter uses Kbit/s the correct value in my case is 500</p>
<pre>scp -l 500 big-file.tar someuser@someserver.com:backups/.</pre>


<p>Related posts:<ol><li><a href='http://www.btaz.com/unixlinux/how-to-automatically-provide-an-answer-to-unix-commands/' rel='bookmark' title='Permanent Link: How to automatically provide an answer to Unix commands'>How to automatically provide an answer to Unix commands</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.btaz.com/unixlinux/how-to-control-or-throttle-scp-file-transfer-speed/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to automatically provide an answer to Unix commands</title>
		<link>http://www.btaz.com/unixlinux/how-to-automatically-provide-an-answer-to-unix-commands/</link>
		<comments>http://www.btaz.com/unixlinux/how-to-automatically-provide-an-answer-to-unix-commands/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 23:29:40 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Unix/Linux]]></category>
		<category><![CDATA[automatically]]></category>
		<category><![CDATA[confirm]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[cp]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[yes]]></category>

		<guid isPermaLink="false">http://www.btaz.com/?p=73</guid>
		<description><![CDATA[How do you automatically provide an answer to a Unix command for example cp: overwrite `destination/./b.txt'? Many Unix/Linux commands that operate on files may stop and ask for confirmation for each file before completing an action. Of course for many Unix/Linux commands there are parameters that allows you to specify the desired behavior, but there [...]


Related posts:<ol><li><a href='http://www.btaz.com/unixlinux/unixlinux-sort-multiple-columns-tab-delimited-and-reverse-sort-order/' rel='bookmark' title='Permanent Link: Unix/Linux Sort Multiple Columns, Tab Delimited and Reverse Sort Order'>Unix/Linux Sort Multiple Columns, Tab Delimited and Reverse Sort Order</a></li>
<li><a href='http://www.btaz.com/unixlinux/how-to-control-or-throttle-scp-file-transfer-speed/' rel='bookmark' title='Permanent Link: How to control or throttle SCP file transfer speed'>How to control or throttle SCP file transfer speed</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>How do you automatically provide an answer to a Unix command for example</p>
<pre>cp: overwrite `destination/./b.txt'?</pre>
<p>Many Unix/Linux commands that operate on files may stop and ask for confirmation for each file before completing an action. Of course for many Unix/Linux commands there are parameters that allows you to specify the desired behavior, but there are other commands that doesn&#8217;t have that capability. Advanced Unix users just look up the &#8220;yes&#8221; command on how to solve this. Here&#8217;s a contrived example where a directory called &#8220;destination&#8221; contains two files &#8220;a.txt&#8221; and &#8220;b.txt&#8221;. We will now copy two files &#8220;b.txt&#8221; and &#8220;c.txt&#8221; from a directory called &#8220;source&#8221;</p>
<pre>|-- destination
|   |-- a.txt
|   `-- b.txt
`-- source
 |-- b.txt
 `-- c.txt</pre>
<p>We&#8217;ll copy the files using the &#8220;cp&#8221; command</p>
<pre>cp source/*.txt destination/.</pre>
<p>When we do this we&#8217;ll get the following response on the command line where the OS is asking us what it want us to do since one file being copied already exist in the destination directory i.e. &#8220;b.txt&#8221;</p>
<pre>cp: overwrite `destination/./b.txt'?</pre>
<p>Unfortunately on Linux the cp command doesn&#8217;t have an option to automatically answer this question with a &#8216;y&#8217; or &#8216;n&#8217; answer. There&#8217;s more than one solution to this problem depending on what you want to do, and one solution is to use the Unix &#8220;yes&#8221; command. This command will output a string repeatedly until killed. If we want to avoid overwriting any of the files in the destination directory we could use the &#8220;yes&#8221; command to answer all questions with a no &#8220;n&#8221;. Below you can see how the &#8220;yes&#8221; command is used to automatically provide the answer &#8220;n&#8221; to the &#8220;cp&#8221; command. Use &#8220;man yes&#8221; for more information about the &#8220;yes&#8221; command.</p>
<pre>yes n | cp source/*.txt destination/.</pre>
<p>This successfully copies all files with the exception of the &#8220;b.txt&#8221; file which is what we wanted. The &#8220;yes&#8221; command automatically provided the answer &#8220;n&#8221; to the &#8220;cp&#8221; command question whether or not we wanted to overwrite the destination file.</p>
<p>In the case that you mess up when you use the &#8220;yes&#8221; command for example by typing this:</p>
<pre>yes bingo</pre>
<p>The &#8220;yes&#8221; command will output the word &#8220;bingo&#8221; repeatedly to the screen. You can stop it by typing Control-C or in a different shell type &#8220;pgrep yes&#8221; which will display the &#8220;yes&#8221; commands PID (process ID) which you can use to kill the &#8220;yes&#8221; command by typing &#8220;kill &lt;PID&gt;&#8221;</p>
<pre>pgrep yes</pre>
<p>In my case it returned 24940 so I killed it with:</p>
<pre>kill 24940</pre>


<p>Related posts:<ol><li><a href='http://www.btaz.com/unixlinux/unixlinux-sort-multiple-columns-tab-delimited-and-reverse-sort-order/' rel='bookmark' title='Permanent Link: Unix/Linux Sort Multiple Columns, Tab Delimited and Reverse Sort Order'>Unix/Linux Sort Multiple Columns, Tab Delimited and Reverse Sort Order</a></li>
<li><a href='http://www.btaz.com/unixlinux/how-to-control-or-throttle-scp-file-transfer-speed/' rel='bookmark' title='Permanent Link: How to control or throttle SCP file transfer speed'>How to control or throttle SCP file transfer speed</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.btaz.com/unixlinux/how-to-automatically-provide-an-answer-to-unix-commands/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

