Bash Tricks: Delete the Last Substring from a Delimited String

I wrote a post about how to print just the last substring of a delimited string. Instead if you’d want to delete just the last substring, and keep rest of the string intact, thats a little trickier.

Here’s how you do it.

@~$ echo "abc:cde:efg" | awk 'BEGIN {FS=ORS=":"} {for(i=1;i<NF;i++) print $i}' | sed 's/$/\n/'

FS is the field separator, while ORS is the output record separator. ORS by default is the new line character \n. What we are doing here is splitting the string using awk, and then printing all but the last records i < NF. The sed statment is just adding a newline at the end.

If you want to skip the trailing delimiter, you could use the code below.

@~$ echo "abc:cde:efg" | awk 'BEGIN {FS=ORS=":"} {for(i=1;i<NF;i++) print $i}' | sed 's/:$/\n/'

Here the sed statement replaces the trailing : with a newline. To skip the newline, you can change the sed statement to sed 's/:$//'.

Bash Tricks: Split / Cut a String with Multi Character Delimiters Using AWK

Some time back I wrote this post showing how to split a string into substrings separated by multi character delimiters. Didn’t realize then that there’s a much easier solution using awk. Using the same example as used in the previous post, here’s the solution.

echo "abcd<>efgh<>ijkl<>mn op<>qr st<>uv wx<>yz" | awk 'BEGIN {FS="<>"} {for(i=1;i<=NF;i++)print $i}'

The delimiter here is "<>".

This will print out all the substrings. If you want an individual substring, you can use something like

echo "abcd<>efgh<>ijkl<>mn op<>qr st<>uv wx<>yz" | awk 'BEGIN {FS="<>"} {print $1}'
echo "abcd<>efgh<>ijkl<>mn op<>qr st<>uv wx<>yz" | awk 'BEGIN {FS="<>"} {print $2}'

Thats how easy it is.

New Technical Documentation Sites for Open Source Software

I dislike the documentation sites that most open source softwares have, they usually lack character. However the good news is that since a lot of documentation is in docbook it can be easily converted to HTML, PDF etc very easily and you can style it the way you like it. I have been working on docbook and docbook conversion for the past 3 months and have learnt enough to make decent looking documentation. So I decided to convert some open source documentation and slap some css on it. Here are the preliminary results: Continue reading

Ruby 1.9.2 Final Build for Ubuntu Lucid Uploaded to Launchpad PPA

This has been long overdue. I just updated my ruby 1.9.2 ppa to the final ruby 1.9.2 release. I borrowed the patches from the upstream maverick release and added a couple more patches to get it work on Ubuntu Lucid. The patching system has now changed from dpatch to quilt. Please download it and enjoy it and let me know if things work fine. I checked my rails applications and they all are working fine with the new release. Here are the instructions to use the Ruby 1.9.2 PPA. Continue reading

WordPress Tricks: Pretty Breadcrumbs for Theme Hybrid based Child Themes

I was bored with the default breadcrumbs that come with Theme Hybrid. Wanted something new. I looked up some tutorials for nice looking breadcrumbs. Found this one which I liked quite a lot and decided to implement it for Humbug. Usually, things are very simple with Theme Hybrid, but this one was slightly ugly. Here’s the code that I used: Continue reading

WordPress Plugin: Remove Author Column on Edit Posts Page

This wordpress plugin removes the author column on Edit Posts page (for custom post types as well). The author column doesn’t make sense on single author blogs. By removing it, you can save precious real estate and use other useful plugins like Simply Show Ids.

This is my first wordpress plugin. Just 6 lines of php code :). I spent more time writing the readme.txt than I did to write this plugin.