Professional Web Applications Themes

Need some help with string manipulation - Linux / Unix Administration

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!...

  1. #1

    Default Need some help with string manipulation

    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!



    Ramesh Guest

  2. #2

    Default Re: Need some help with string manipulation

    On Thu, 2005-12-01 at 23:32 +0000, Ramesh N wrote: 
    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.
    ++


    S. Guest

  3. #3

    Default Re: Need some help with string manipulation

    On 2005-12-01, Ramesh N wrote: 

    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 Guest

  4. #4

    Default Re: Need some help with string manipulation

    Ramesh N wrote: 

    Portable shell (even old bourne shell):

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

  5. #5

    Default Re: Need some help with string manipulation

    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.

    john Guest

Similar Threads

  1. Classic ASP String Manipulation - NOT .net
    By James in forum ASP.NET Web Services
    Replies: 6
    Last Post: February 16th, 01:17 PM
  2. String Manipulation - Easiest Way?
    By Nanwedar in forum Macromedia ColdFusion
    Replies: 5
    Last Post: February 24th, 04:01 PM
  3. Another String Manipulation Question
    By Bill in forum Coldfusion - Advanced Techniques
    Replies: 4
    Last Post: February 21st, 03:31 AM
  4. String manipulation
    By Jerry Preston in forum PERL Beginners
    Replies: 2
    Last Post: January 19th, 01:27 PM
  5. Replies: 2
    Last Post: July 20th, 06:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139