Unix administration - Need some help with string manipulation

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > December 2005 > Need some help with string manipulation





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Need some help with string manipulation
Ramesh N

2005-12-02, 5:54 pm

Hi Gurus,

I have a small requirement as below.

From a particular program i get the FQDN of a particular host, for example:

host1toronto.plains.com

now i would like to truncate everything including and after the toronto.

And assing the remaining string (i.e host1) alone to a variable.

Could some one throw in a quick awk script and help me?

Thanks much in advance!



S. Anthony Sequeira

2005-12-02, 5:54 pm

On Thu, 2005-12-01 at 23:32 +0000, Ramesh N wrote:
> Hi Gurus,
>
> I have a small requirement as below.
>
> From a particular program i get the FQDN of a particular host, for example:
>
> host1toronto.plains.com
>
> now i would like to truncate everything including and after the toronto.
>
> And assing the remaining string (i.e host1) alone to a variable.
>
> Could some one throw in a quick awk script and help me?
>
> Thanks much in advance!
>

You don't need awk:

$ echo $SHELL
/bin/bash
$ FQDN=host1toronto.plains.com
$ echo ${FQDN%toronto*}
host1

the korn shell, and probably others (zsh?) will do the same.
--
S. Anthony Sequeira
++
So many men; so little time.
++


Chris F.A. Johnson

2005-12-02, 5:54 pm

On 2005-12-01, Ramesh N wrote:
> Hi Gurus,
>
> I have a small requirement as below.
>
> From a particular program i get the FQDN of a particular host, for example:
>
> host1toronto.plains.com
>
> now i would like to truncate everything including and after the toronto.
>
> And assing the remaining string (i.e host1) alone to a variable.
>
> Could some one throw in a quick awk script and help me?


In a POSIX shell (e.g., bash, ash, ksh), you don't need awk to do
that:

FQDN=host1toronto.plains.com
trunc=${FQDN%toronto*}

Using awk you could do:

trunc=$(printf "%s\n" "$FQDN" | awk -F "toronto" '{print $1}')

Or:

trunc=$(printf "%s\n" "${FQDN}" | awk '{sub(/toronto.*/,"");print}')

Or with sed:

trunc=$(printf "%s\n" "${FQDN}" | sed 's/toronto.*//')

Note that, if there are two instances of "toronto" in your $FQDN,
the awk and sed solutions will truncate from the first one.


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Chris Cox

2005-12-04, 2:48 am

Ramesh N wrote:
> Hi Gurus,
>
> I have a small requirement as below.
> From a particular program i get the FQDN of a particular host, for example:
> host1toronto.plains.com
>
> now i would like to truncate everything including and after the toronto.
> And assing the remaining string (i.e host1) alone to a variable.
> Could some one throw in a quick awk script and help me?
>
> Thanks much in advance!
>


Portable shell (even old bourne shell):

fqdn="host1toronto.plains.com"
hostname=`expr "$fqdn" : '\([^.]*\)'`
john

2005-12-05, 5:59 pm

a cheater awk script would be awk -F. '{print $1}'

Changes the seperator to a '.' (dot) and then prints the first column.
This can be assigned to a variable if need be.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com