<?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>No Znx! &#187; shell</title>
	<atom:link href="http://znx.no/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://znx.no</link>
	<description>the pigeons!!!!</description>
	<lastBuildDate>Tue, 20 Dec 2011 22:42:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Arch / Yum</title>
		<link>http://znx.no/2011/12/arch-yum/</link>
		<comments>http://znx.no/2011/12/arch-yum/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 23:52:29 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[archlinux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=364</guid>
		<description><![CDATA[I recently switched to Archlinux and I have to say I love it a great deal. I really like the simplicity of it, unfortunately due to my long standing usage or yum I have been spoilt. It means that I find myself wishing to have commands like yum install package, so switching pacman&#8217;s -S was [...]]]></description>
			<content:encoded><![CDATA[<p>I recently switched to Archlinux and I have to say I love it a great deal. I really like the simplicity of it, unfortunately due to my long standing usage or <code>yum</code> I have been spoilt. It means that I find myself wishing to have commands like <code>yum install package</code>, so switching pacman&#8217;s <code>-S</code> was a little too much. I therefore wrote a wrapper to make it easier for me. Maybe it can help others just starting out in the path to using Archlinux.</p>
<p>In preparation for this, you will need to install <code>yaourt</code> and enable it with sudo access. Either that or replace all the references to <code>yaourt</code> below with <code>pacman</code>.</p>
<p>Drop this into your <i>~/.bashrc</i> (or zshrc):</p>
<pre>function arch() {
        if [ -z "$1" ]; then
                echo "arch <command> <blah...>" >&#038;2
                return -1
        fi

        case $1 in
                upgrade)
                        # Synchronize, upgrade
                        shift
                        sudo yaourt -Syu $@
                        ;;
                install)
                        # Install stuff
                        shift
                        sudo yaourt -S $@
                        ;;
                localinstall)
                        # Install file
                        shift
                        sudo yaourt -U $@
                        ;;
                remove)
                        # Remove
                        shift
                        sudo yaourt -R $@
                        ;;
                fullremove)
                        # Remove file
                        shift
                        sudo yaourt -Rns $@
                        ;;
                info)
                        # Display information
                        shift
                        yaourt -Si $@
                        ;;
                search)
                        # Search
                        shift
                        yaourt -Ss $@
                        ;;
        esac
}</pre>
<p>Now you can do things like:</p>
<pre>$ arch install git
$ arch remove git
$ arch info git
$ arch search git</pre>
<p>Hope its handy!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2011/12/arch-yum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSH Keys</title>
		<link>http://znx.no/2011/04/ssh-keys/</link>
		<comments>http://znx.no/2011/04/ssh-keys/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 22:05:26 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[SSH]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=342</guid>
		<description><![CDATA[I have for the longest time been using public key authentication on almost every shell I use. One of the things that most people do with these keys is set them up without passwords to allow them to open connections to shells without typing passwords. Whilst this increases the ease of use it obviously misses [...]]]></description>
			<content:encoded><![CDATA[<p>I have for the longest time been using public key authentication on almost every shell I use. One of the things that most people do with these keys is set them up without passwords to allow them to open connections to shells without typing passwords. Whilst this increases the ease of use it obviously misses the point. That sort of short-cut is taken without considering how you can use passwords but still have the ease of use.</p>
<p>Lets see how SSH works without any setup:</p>
<pre>$ ssh znx@work.host.com
 znx@work.host.com's password: <b>(password)</b></pre>
<p>Type in your password and you get a shell. Now lets generate some keys and login with those.</p>
<pre>$ cd ~/.ssh
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/mark/.ssh/id_rsa): id_workhost
Enter passphrase (empty for no passphrase): <b>(passphrase)</b>
Enter same passphrase again: <b>(passphrase)</b>
Your identification has been saved in id_workhost.
Your public key has been saved in id_workhost.pub.
The key fingerprint is:
75:9c:c0:a3:52:7e:92:3c:f7:9d:c0:ff:0b:18:8c:48 znx@home.local
The key's randomart image is:
+--[ RSA 2048]----+
|         ..      |
|        . oo .   |
|       E o.o+    |
|      o O.=.o    |
|       oS* + + . |
|            + +  |
|           . . . |
|              . .|
|               ..|
+-----------------+</pre>
<p>So the private key is called <i>id_workhost</i> and the public key is called <i>id_workhost.pub</i>. The private key should be kept safe at all times; the public key however is what we need to put out onto the other machine. If you have ssh-copy-id you can use that instead of the follow. So lets do that now:</p>
<pre>$ scp ~/.ssh/id_workhost.pub znx@work.host.com:
znx@work.host.com's password:  <b>(password)</b>
id_workhost.pub                                100% 1766     1.7KB/s   00:00
$ ssh znx@work.host.com
znx@work.host.com's password:  <b>(password)</b>
work$ cat id_workhost.pub >> ~/.ssh/authorized_keys
work$ rm id_workhost.pub
work$ logout
$ ssh -i ~/.ssh/id_workhost znx@work.host.com
Enter passphrase for key '/home/znx/.ssh/id_workhost': <b>(passphrase)</b>
Last login: Wed Apr  6 08:25:16 2011 from home.local
work$</pre>
<p>So we are now using keys to login rather than passwords but it still requires a passphrase to be type in. The next step is to look at <b>ssh-agent</b>. This tool will hold keys in memory and allow thus allowing you to login without needing to unlock the key. So lets run it and see what it does:</p>
<pre>$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-SdjNGn3604/agent.3604; export SSH_AUTH_SOCK;
SSH_AGENT_PID=3605; export SSH_AGENT_PID;
echo Agent pid 3605;</pre>
<p>Not exactly useful until you spot that it is actually setting up bash variables. So the way we want to run ssh-agent is as follows:</p>
<pre>$ eval `ssh-agent`
Agent pid 3609
$ echo $SSH_AGENT_PID
3609</pre>
<p>That&#8217;s more like it, so once you have a running agent you can now start to add keys into its memory.</p>
<pre>$ ssh-add ~/.ssh/id_workhost
Enter passphrase for /home/mark/.ssh/id_workhost: <b>(passphrase)</b>
Identity added: /home/mark/.ssh/id_workhost (/home/mark/.ssh/id_workhost)</pre>
<p>With the key added we can now login to the host with the unlocked key:</p>
<pre>$ ssh znx@work.host.com
Last login: Wed Apr  6 22:14:49 2011 from home.local
work$</pre>
<p>Now that is a login without a password or a passphrase because the key is unlocked and held by ssh-agent. Whilst <b>ssh-agent</b> is useful if you retain one shell open all the time, the moment you close that shell you lose the variables of the running ssh-agent which means that you will not be able to use it. This also means that if you are running scripts (say via cron) that you can no longer automate the logins. So back to keys without passphrase? NO!</p>
<p>There is a very nice tool that can help us out and it is called <b>keychain</b>. Simply add this to your <i>~/.bash_profile</i>:</p>
<pre>keychain --quiet ~/.ssh/id_workhost
source ~/.keychain/*-sh</pre>
<p>You should also add this to your ~/.bashrc:</p>
<pre>source ~/.keychain/*-sh</pre>
<p>Now login to your shell again and you should see that you are prompted to put in passwords. This is keychain loading your keys in. It looks something like this:</p>
<pre>Identity added: /home/mark/.ssh/id_mine (/home/mark/.ssh/id_mine)</pre>
<p>You will see that you can now login without passphrase to your host again. Indeed all the shells you open will now grab the keychain information and login immediately.</p>
<p>This means that you will only get prompted for passphrases once when you login and then you will have the ability to login to your shells without passphrases. Obviously you should add keys for your regular hosts. You can add keys on the fly with keychain at the command line:</p>
<pre>$ keychain ~/.ssh/id_someotherkey</pre>
<p>And it will be stored until your ssh-agent is closed. If you wish to clear the keys you have added then simply do:</p>
<pre>$ keychain --clear</pre>
<p>What all this means is that you can now run scripts with passphrase protected keys because all you need to do is source the running ssh-agent within your script:</p>
<pre>#!/bin/sh
# Some remote copy script
source ~/.keychain/*-sh

scp znx@work.host.com:nightly-copy.file local/nightly-copy.file</pre>
<p>As long as the key is load the script will work.</p>
<p>Doing this ensures that even if the private key falls into the wrong hands that it is still secured with a passphrase and you haven&#8217;t just opened up your remote hosts to attack.</p>
<p>There is solutions for this under Windows as well; the PuTTY installer will provide you with a tool called Pageant. This works in an identical way to ssh-agent; loading keys into its memory to allow PuTTY to login with unlocked keys.</p>
<p>Check out some of the other SSH tips I&#8217;ve published which will ease the logins even further!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2011/04/ssh-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSH and Limits</title>
		<link>http://znx.no/2011/01/ssh-and-limits/</link>
		<comments>http://znx.no/2011/01/ssh-and-limits/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 18:50:34 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[SSH]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=306</guid>
		<description><![CDATA[I was tasked to look at why a limit wasn&#8217;t being applied to a shell when using ssh. To let you understand lets have a look at what was being seen: [user@host]$ ssh test -l user user@test's password: [user@test]$ ulimit -n 1024 [user@test]$ cat /etc/security/limits.conf * soft nofile 4096 * hard nofile 4096 However switching [...]]]></description>
			<content:encoded><![CDATA[<p>I was tasked to look at why a limit wasn&#8217;t being applied to a shell when using <code>ssh</code>. To let you understand lets have a look at what was being seen:</p>
<pre>[user@host]$ ssh test -l user
user@test's password:
[user@test]$ ulimit -n
1024
[user@test]$ cat /etc/security/limits.conf
* soft nofile 4096
* hard nofile 4096</pre>
<p>However switching to the same user you will see:</p>
<pre>[user@test]$ su - user
Password:
[user@test]$ ulimit -n
4096</pre>
<p>That the limit isn&#8217;t respect when logging in via SSH but when we switch user it is applied.</p>
<p>The reason for this is rather simple; the SSH is opening a shell that isn&#8217;t a login shell and therefore limits is not being applied. To correct this simply edit your <em>sshd_config</em> file and set it to use login shells.</p>
<pre>[root@test]$ grep -i uselogin /etc/ssh/sshd_config
#UseLogin no</pre>
<p>You can see that the entry is set by default to <strong>no</strong>; so simply edit the line and alter it to yes:</p>
<pre>[root@test]$ sed -i.bak 's/#UseLogin no/UseLogin yes/' /etc/ssh/sshd_config
[root@test]$ grep -i uselogin /etc/ssh/sshd_config
UseLogin yes</pre>
<p>Now reload your <em>sshd</em>.</p>
<pre>[root@test]$ /etc/init.d/sshd reload
 /etc/init.d/sshd reload
Reloading sshd:                                            [  OK  ]</pre>
<p>And test again:</p>
<pre>[user@host]$ ssh test -l user
user@test's password:
[user@test]$ ulimit -n
4096</pre>
<p>That&#8217;s it! Sometimes odd behaviour like this can be difficult to track down without having a good read of configuration files.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2011/01/ssh-and-limits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unrar Bug</title>
		<link>http://znx.no/2010/02/unrar-bu/</link>
		<comments>http://znx.no/2010/02/unrar-bu/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 01:29:50 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[rpm]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=274</guid>
		<description><![CDATA[So I was recently scripting with the unrar tool and discovered something stupid: unrar t file.rar if &#91; $? -eq 0 &#93;; then echo &#34;Rar file is good?&#34; fi However it was returning zero all the time, even when the file wasn&#8217;t a rar: # unrar t file.rar &#160; UNRAR 3.80 freeware Copyright &#40;c&#41; 1993-2008 [...]]]></description>
			<content:encoded><![CDATA[<p>So I was recently scripting with the unrar tool and discovered something stupid:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">unrar t file.rar
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Rar file is good?&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>However it was returning zero all the time, even when the file wasn&#8217;t a rar:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># unrar t file.rar</span>
&nbsp;
UNRAR <span style="color: #000000;">3.80</span> freeware      Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">1993</span>-<span style="color: #000000;">2008</span> Alexander Roshal
&nbsp;
file.rar is not RAR archive
<span style="color: #666666; font-style: italic;"># echo $?</span>
<span style="color: #000000;">0</span></pre></div></div>

<p>So it fails the test but returns zero regardless. This makes it very unhelpful for using in scripting. Fortunately enough a mate on IRC discovered that his version did.</p>
<p>So I first download the existing SRPM and installed it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># yumdownloader --source unrar</span>
<span style="color: #666666; font-style: italic;"># rpm -i unrar*.srpm</span></pre></div></div>

<p>Then I installed that and simply modified  so I downloaded the latest, created a RPM and installed.</p>
<p>I have submitted the updated spec file to <a href='https://bugzilla.rpmfusion.org/show_bug.cgi?id=1091'>RPMfusion</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2010/02/unrar-bu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Import Missing GPG Keys</title>
		<link>http://znx.no/2010/01/import-missing-gpg-keys/</link>
		<comments>http://znx.no/2010/01/import-missing-gpg-keys/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 17:58:39 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[gpg]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://znx.no/?p=231</guid>
		<description><![CDATA[Sometimes when people are using Linux they will find that they come across a simple security check that confirms the authenticity of the RPM/Deb files that they are installing. Every so often the key is missing but that is easy to remedy. Here I will use an example with the YUM install of Adobe Flash: [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when people are using Linux they will find that they come across a simple security check that confirms the authenticity of the RPM/Deb files that they are installing. Every so often the key is missing but that is easy to remedy. Here I will use an example with the YUM install of Adobe Flash:</p>
<pre>[root@marine]# ls
adobe-release-i386-1.0-1.noarch.rpm
[root@marine]# yum localinstall adobe-release-i386-1.0-1.noarch.rpm
Loaded plugins: fastestmirror, priorities
Setting up Local Package Process
Examining adobe-release-i386-1.0-1.noarch.rpm: adobe-release-i386-1.0-1.noarch
Marking adobe-release-i386-1.0-1.noarch.rpm to be installed
Loading mirror speeds from cached hostfile
* addons: anorien.csc.warwick.ac.uk
* base: anorien.csc.warwick.ac.uk
* centosplus: anorien.csc.warwick.ac.uk
* contrib: anorien.csc.warwick.ac.uk
* extras: anorien.csc.warwick.ac.uk
* rpmforge: fr2.rpmfind.net
* updates: anorien.csc.warwick.ac.uk
588 packages excluded due to repository priority protections
Resolving Dependencies
--&gt; Running transaction check
---&gt; Package adobe-release-i386.noarch 0:1.0-1 set to be updated
--&gt; Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package              Arch     Version Repository                          Size
================================================================================
Installing:
adobe-release-i386   noarch   1.0-1   /adobe-release-i386-1.0-1.noarch   1.9 k

Transaction Summary
================================================================================
Install      1 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total size: 1.9 k
Is this ok [y/N]: y
Downloading Packages:
warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID f6777c67

Public key for adobe-release-i386-1.0-1.noarch.rpm is not installed</pre>
<p>As you can see it attempted to install but because the key was missing it refused to install. So what do we do to find that key? First we must download the key from somewhere, there is various key servers around the world, some of the most popular are (note hkp is the HTTP keyserver protocol):</p>
<ul>
<li>hkp://subkeys.pgp.net</li>
<li>hkp://pgp.mit.edu</li>
<li>hkp://keys.gnupg.net</li>
<li>hkp://wwwkeys.uk.pgp.net  (where UK can be replaced by any country code)</li>
</ul>
<p>So lets search for the missing key:</p>
<pre>[root@marine]# gpg --keyserver hkp://wwwkeys.uk.pgp.net --recv-keys f6777c67
gpg: requesting key F6777C67 from hkp server wwwkeys.uk.pgp.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key F6777C67: public key "Adobe Systems Incorporated (Linux RPM
  Signing Key) <secure@adobe.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1</pre>
<p>So now we have the key and see that it is indeed Adobe&#8217;s key. Now all we need to do is add into RPM:</p>
<pre>[root@marine]# gpg --armor --export f6777c67 >tmp-gpg.key
[root@marine]# rpm --import tmp-gpg.key</pre>
<p>Simple as that, the install will work now!</p>
<p>This post is a refreshed look at a <a href='http://znx.no/2009/08/gpg-errors/'>previous</a> post on the same matter.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2010/01/import-missing-gpg-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Skinning A Cat</title>
		<link>http://znx.no/2009/05/skinning-a-cat/</link>
		<comments>http://znx.no/2009/05/skinning-a-cat/#comments</comments>
		<pubDate>Sun, 17 May 2009 02:02:36 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=116</guid>
		<description><![CDATA[I was recently discussing with someone about sed usage. They were having difficultly creating an appropriate regex to handle their problem: (Orlando) sed regex kicks my ass. I like to remove the second : in the line. So that in 123:456:6789 it will only returns 123:456. I can find the first :, but I have [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently discussing with someone about <code>sed</code> usage. They were having difficultly creating an appropriate regex to handle their problem:</p>
<pre>(Orlando) sed regex kicks my ass.  I like to remove the second : in the
line. So that in 123:456:6789 it will only returns 123:456.  I can find the
first :, but I have not been able to use s/:{2}// to find the second
one, and remove the rest.</pre>
<p>I enjoy regex (I know I&#8217;m weird, leave me alone), so I was able to provide an answer to this problem:</p>
<pre>(@znx) Orlando: the trick is to use [^:] and \1 ..
(@znx) like:   s/\(:[^:]*\):.*/\1/</pre>
<p>Now obviously at first glance this regex could be a bit intimating to someone who is still picking up the skills but as with most things if we break it down it becomes easier.</p>
<p>Working out to in, <code>\(    \):.*</code>, that says match something with <strong>:</strong> at the end and all the character after it. The <strong>.</strong> is a special meaning &#8220;any character&#8221; and <strong>*</strong> to match multiple characters. The first match will be stored by <code>sed</code> and assigned into the <strong>\1</strong> for the replacement (that is what the brackets do). Inside the brackets we have <code>:[^:]*</code>. The sequence <strong>[^ ]</strong> is a negated list, that means that we are asking it to match everything that is NOT inside the list, in this case <strong>:</strong>.</p>
<p>Putting it altogether we are saying: Match a leading : and a trailing : with any characters after it. Placing the contents between the two <strong>:</strong> in memory. Then finally we replace the contents.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/\(:[^:]*\):.*/\1/'</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>Sucess, however as with most things, there is more than one way to skin a cat and regex is rarely the prettiest method. So what other ways can we solve this problem?</p>
<p>With AWK:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $1&quot;:&quot;$2}'</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>With <code>cut</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d: -f1,<span style="color: #000000;">2</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>As always, experimentation with the mass of GNU tools you can find on your system will bring a greater deal of power to your tool chest. Mind you, then I wouldn&#8217;t get complements for helping would I?</p>
<pre>(Orlando) Wow, When I grow up, I like to remember this thing like you do. <img src='http://znx.no/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </pre>
<p>Haha, till next time!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/05/skinning-a-cat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Oddity</title>
		<link>http://znx.no/2009/02/file-oddity/</link>
		<comments>http://znx.no/2009/02/file-oddity/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 18:10:38 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://znx.no/?p=102</guid>
		<description><![CDATA[Today at work I was attempting to parse a file and discovered something odd happening. When I simply viewed the file with cat, I could see this: &#60;html&#62;&#60;head&#62;&#60;title&#62;Status&#60;/title&#62;&#60;/head&#62; &#60;table&#62; &#60;tr&#62;&#60;td&#62;Failed&#60;/td&#62;&#60;td&#62;Backup Group&#60;/td&#62;&#60;/tr&#62; &#60;tr&#62;&#60;td&#62;Success&#60;/td&#62;&#60;td&#62;Another Backup Group&#60;/td&#62;&#60;/tr&#62; &#60;/table&#62; &#60;/body&#62; &#60;/html&#62; Nothing odd there, the file is normal but when I tried this command: $ grep -i failed status.html [...]]]></description>
			<content:encoded><![CDATA[<p>Today at work I was attempting to parse a file and discovered something odd happening. When I simply viewed the file with cat, I could see this:</p>
<pre>&lt;html&gt;&lt;head&gt;&lt;title&gt;Status&lt;/title&gt;&lt;/head&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Failed&lt;/td&gt;&lt;td&gt;Backup Group&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Success&lt;/td&gt;&lt;td&gt;Another Backup Group&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Nothing odd there, the file is normal but when I tried this command:</p>
<pre>$ grep -i failed status.html
$</pre>
<p>Huh? No output, suggesting that there is no lines with the words failed on them. The same occurs with awk and sed, indeed I could not find any tool to be able to grep out the status. So the next step was to check what was odd about the file:</p>
<pre>$ file status.html
status.html: HTML document text</pre>
<p>Still, nothing unusual. So now I am in full head scratching mode, I open up the file with vim to see if I can discover anything strange about the file but nothing. At this point I happened to switch to more rather than cat and the result was the start of how I solved it.</p>
<pre>$ more status.html
��&lt;

$</pre>
<p>Ah, so now I can see why grep and the others cannot view anything in the file. So this time I switch to vim again and check what file encoding we have:</p>
<pre>:set fileencoding
fileencoding=ucs-2le</pre>
<p>For those that are unaware, this is UCS-2 (little endian), also know as <a href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a>. So the issue was simply that we had UTF-16 characters, now for the trick to get around it:</p>
<pre>$ iconv -f UTF-16 -t UTF-8 status.html | grep -i failed
&lt;tr&gt;&lt;td&gt;Failed&lt;/td&gt;&lt;td&gt;Backup Group&lt;/td&gt;&lt;/tr&gt;
$</pre>
<p>Tada. Once more a solution!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/02/file-oddity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

