Geek Lair

Extracting a substring from a starting point using awk

leave a comment »

If you have a string like this (this is how versions look in Clearcase):


and want to extract the string 78 (i.e, the version no.), you can do it this way:

echo "/root_path/stream/version_name/78" | awk '{where = match($0,"\/[^\/]*$"); print substr($0,where+1)}'

We get the position of the last occurrence of “/” and use substr to extract the string after that position till end of string.


Written by anonir

December 29, 2009 at 19:07

Posted in Linux

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: