Otaqui.Com

Pete Otaqui’s blog about web development and everything else

Archive for the ‘command-line’ tag

Comment And Uncomment All Lines in a Linux File with Sed

leave a comment

Just a couple of sed one-liners for adding and removing comments in the form of # marks (in the case of my ~/.ssh/config file). I use this to toggle proxy settings (with connect.c) and it’s run by the awesome MarcoPolo location-aware app for OS X. Both of these are safe to run repeatedly (you won’t end up with multiple # marks or anything).

First adding comments, which means a “#” mark at the start of every line
sed -i '' 's/^\([^#]\)/#\1/g' ~/.ssh/config

Second removing the comments, just stripping out the “#” marks.
sed -i '' 's/^#//g' ~/.ssh/config

A few things to note here, on the assumption that you can see how Regular Expressions work and are vaguely familiar with sed’s /find/replace/ style syntax:

  1. the “-i” flag means “edit in place” and requires an extra argument for the backup file’s extension. I’ve given an empty string so that no backup is made
  2. sed requires escaping of capturing parentheses, hence the \( and \) in the first example
  3. the final ‘g’ at the end of the expressions means “global”, i.e. replace all occurences

Written by pete

February 9th, 2010 at 5:21 pm

Posted in Professional

Tagged with ,

Cucumber with Webrat and Mechanize on CentOS 5

leave a comment

Thanks to Chris for this one.

The trick is to use JRuby and its gems, and also to install libxml2-devel

$ cd ~
$ wget http://jruby.kenai.com/downloads/1.4.0/jruby-bin-1.4.0.tar.gz
$ tar -C /usr/local/ -xzvf jruby-bin-1.4.0.tar.gz

You should now have Jruby. Add it’s bin directory to your path, presumably in your ~/.bash_profile file:
$ export PATH=$PATH:/usr/local/jruby-1.4.0/bin
$ jruby -v

Now install some mechanize requirements:
$ sudo yum install libxml2-devel libxslt-devel

If you don’t already have the “normal” ruby installed, you can get to the jruby “gem” command by just typing “gem”. If you do have ruby, or want to be absolutely sure, type the commands like this:
$ jruby -S gem install cucumber mechanize webrat

You’re good to go! You should now be able to run your features with:

$ cucumber features/

Written by pete

November 20th, 2009 at 9:55 pm

Posted in Professional

Tagged with , ,

Listing files with the path in Bash

leave a comment

This little script will print a list of the current files in a directory, prefixed with the full path:

#!/bin/sh
curdir=`pwd`
if [ $# -eq 1 ]
then
usrdir="$1"
ls | xargs -I % echo `pwd`/% | sed -e s!$usrdir!!
else
ls | xargs -I % echo `pwd`/%
fi

You can optionally provide an initial part of the path to ignore. I use this when writing code change emails where I have to list the updated files within a codebase (and I’ve touched most files in a given directory) and I clip the local path up to the root of the codeset. Assuming the script is saved as “lspath”:

lspath /Users/pete/Sites/project/trunk

Even handier is piping it into the OS X clipboard for easy pasting:

lspath /Users/pete/Sites/project/trunk | pbcopy

Written by pete

March 1st, 2008 at 3:43 am

Posted in Professional

Tagged with ,

Split and Rename text files from the Bash command line

one comment

I occasionally read text files (usually downloaded from the Gutenberg Project) on my mobile phone. This is not the best user experience, but bearable and very useful at times.

The most annoying issue is the incredibly slow management of very large text files (sometimes greater than 1Mb) that my phone displays – so i decided to rename them.

Using my mac, I came up with this couple of commands to do the work for me, between them they will split all text files into 1000 line chunks, and assuming an original filename of “somefile.txt” also rename them to “somefile_XX.txt” where XX is actually two letters denoting the part, starting at aa, ab, ac, etc.

The first command is dead easy and splits the files:

for file in *.txt ; do split $file $file ; done

This will split the files and turn “somefile.txt” into “somefile.txtaa”, “somefile.txtab”, “somefile.txtac” etc. which is obviously not great – so we need the next step:

for file in * ; do mv $file `echo $file |
sed 's/(.*).txt([a-z]{2})/1_2.txt/'` ; done

Now you should have all the files nicely named “somefile_aa.txt”, “somefile_ab.txt”, “somefile_ac.txt” etc.

You might prefer numbers rather than letters, in which case just check out the man page for the ’split’ command.

Written by pete

February 26th, 2008 at 9:17 pm

Posted in Professional

Tagged with ,