<?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>Wojciech Jamrozy blog &#187; swap</title>
	<atom:link href="http://www.wojtekrj.net/tag/swap/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wojtekrj.net</link>
	<description>Blog about computer science, programming and linux</description>
	<lastBuildDate>Sun, 23 Oct 2011 21:57:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[Bash] Script to swap contents of files</title>
		<link>http://www.wojtekrj.net/2008/08/bash-script-to-swap-contents-of-files/</link>
		<comments>http://www.wojtekrj.net/2008/08/bash-script-to-swap-contents-of-files/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 11:45:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[file manipulation]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[swap]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=51</guid>
		<description><![CDATA[There are two files (for example called „f1” and „f2”). We want to swap contents of these files. Here is an easy bash script, which solves this problem: #!/bin/sh # Created by Wojtek Jamrozy (www.wojtekrj.net) mv $1 cop_$1 mv $2 $1 mv cop_$1 $2 Download this file or make a text file with this content [...]]]></description>
			<content:encoded><![CDATA[<p>There are two files (for example called „f1” and „f2”). We want to swap contents of these files. Here is an easy bash script, which solves this problem:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #666666; font-style: italic;"># Created by Wojtek Jamrozy (www.wojtekrj.net)</span>
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$1</span> cop_<span style="color: #007800;">$1</span>
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$2</span> <span style="color: #007800;">$1</span>
<span style="color: #c20cb9; font-weight: bold;">mv</span> cop_<span style="color: #007800;">$1</span> <span style="color: #007800;">$2</span></pre></div></div>

<p>Note: There is a file embedded within this post, please visit this post to download the file.<span id="more-51"></span><br />
Download this file or make a text file with this content named <em>swap </em>and make it executable with command</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x swap</pre></div></div>

<p>Then copy this file to <em>/usr/local/bin</em> directory using:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> swap <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin</pre></div></div>

<p>If you have this file in <em>/usr/local/bin</em> directory (or any other included in<em> $PATH</em> environment variable – command</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$PATH</span></pre></div></div>

<p>will show other directories) you don&#8217;t have to write full path to script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">swap f1 f2</pre></div></div>

<p>will swap contents of files f1 and f2.<br />
Example of usage in shell:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">wojtekrj<span style="color: #000000; font-weight: bold;">@</span>wojtek-laptop:~$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;File 1&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> file1 <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;File 2&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> file2
wojtekrj<span style="color: #000000; font-weight: bold;">@</span>wojtek-laptop:~$ <span style="color: #c20cb9; font-weight: bold;">cat</span> file1
File <span style="color: #000000;">1</span>
wojtekrj<span style="color: #000000; font-weight: bold;">@</span>wojtek-laptop:~$ <span style="color: #c20cb9; font-weight: bold;">cat</span> file2
File <span style="color: #000000;">2</span>
wojtekrj<span style="color: #000000; font-weight: bold;">@</span>wojtek-laptop:~$ swap file1 file2
wojtekrj<span style="color: #000000; font-weight: bold;">@</span>wojtek-laptop:~$ <span style="color: #c20cb9; font-weight: bold;">cat</span> file1
File <span style="color: #000000;">2</span>
wojtekrj<span style="color: #000000; font-weight: bold;">@</span>wojtek-laptop:~$ <span style="color: #c20cb9; font-weight: bold;">cat</span> file2
File <span style="color: #000000;">1</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span></pre></div></div>

<p>is a standard header, which helps bash shell recognize file as a bash script.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mv</span> file1 file2</pre></div></div>

<p>a command, which rename/move file from path <em>file1</em> to <em>file2</em></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #c20cb9; font-weight: bold;">file</span></pre></div></div>

<p>show content of <em>file</em></p>
<p>When the script is executed with two arguments (like in example – <em>swap f1 f2</em>), $1 will contain first  argument &#8211; f1, and $2 second – f2.<br />
When $1 or $2 appears in script, it&#8217;s replaced with content of $1 or $2:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$1</span> cop_<span style="color: #007800;">$1</span> -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> f1 cop_f1
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$2</span> <span style="color: #007800;">$1</span> -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> f2 f1</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> text_to_display</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;text_to_display&quot;</span></pre></div></div>

<p>commands will show text <em>text_to_display</em></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">command</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">file</span></pre></div></div>

<p>will save output of <em>command</em> to file <em>file</em></p>
<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;linkname=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F08%2Fbash-script-to-swap-contents-of-files%2F&amp;title=%5BBash%5D%20Script%20to%20swap%20contents%20of%20files" id="wpa2a_2">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2008/08/bash-script-to-swap-contents-of-files/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

