Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I have a string "aaa://abcxyz/xxxxx" I have to get strings "aaa", "abcxyz" and "/xxxxx" from the string I have. Can you please help me out with this? Thanks and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    13

    Question collecting substrings from the string


    Hi,

    I have a string "aaa://abcxyz/xxxxx"

    I have to get strings "aaa", "abcxyz" and "/xxxxx" from the string I have.

    Can you please help me out with this?

    Thanks and Regards,
    krishna

  2. #2
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    Hi,

    Which language? ... Anyway, the best solution will be based on regular expressions, using parenthesis for memorize the part of the string. For example, in JS you can do:
    Code:
    var parts = /(\w+):\/\/(\w+)(\/\w+)/.exec(pattern)
    and you will have an array with the parts of the expression... there are similar functionalities in almos all modern languages.

    Hugo
    EOF

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    13
    Hi,

    The language is C. I tried the following and it is working.

    string httpUrl("aaa://abcxyz/xxxxx");
    string hostname;
    char *local_path = NULL;
    char *protocol = NULL;
    char *ip = NULL;
    protocol = strstr(httpUrl.c_str(), "//");
    protocol++;
    protocol++;
    local_path = strstr(protocol, "/");
    hostname = httpUrl.substr(protocol - httpUrl.c_str(),local_path - protocol);
    printf("\nprotocol is %s and length is %d\n",protocol,strlen(protocol));
    printf("\nlocal_path is %s and length is %d\n",local_path,strlen(local_path));
    printf("\nhostname is %s and length is %d\n",hostname.c_str(),strlen(hostname.c_str()));

    I just want to know if there is any better way to do this.

    Thanks and Regards,
    krishna
    Last edited by krishna chaithanya; 06-05-2009 at 07:03 AM. Reason: changing the string name

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by hugortega View Post
    Anyway, the best solution will be based on regular expressions
    sometimes the best way is NOT to use regular expression
    Code:
    echo "aaa://abcxyz/xxxxx" | awk 'BEGIN{FS="://|/"}{print $1,$2,$3}'

  5. #5
    Just Joined!
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by krishna chaithanya View Post
    Hi,

    I have a string "aaa://abcxyz/xxxxx"

    I have to get strings "aaa", "abcxyz" and "/xxxxx" from the string I have.

    Can you please help me out with this?

    Thanks and Regards,
    krishna
    you could use sed, eg

    str="aaa://abcxyz/xxxxx"
    str1=$(echo $str| sed's?^\([^:]*\):.*$?\1?')
    str2=$(echo $str| sed 's?^[^/]*//\([^/]*\)/.*$?\1?')
    str3=$(echo $str| sed 's?^[^/]*//[^/]*\(/.*\)$?\1?')

    your first substring is stored in $str1, second substring in $str2, etc

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by vonbiber View Post
    you could use sed, eg

    str="aaa://abcxyz/xxxxx"
    str1=$(echo $str| sed's?^\([^:]*\):.*$?\1?')
    str2=$(echo $str| sed 's?^[^/]*//\([^/]*\)/.*$?\1?')
    str3=$(echo $str| sed 's?^[^/]*//[^/]*\(/.*\)$?\1?')

    your first substring is stored in $str1, second substring in $str2, etc
    no need to call sed so many times,
    Code:
    # echo "aaa://abcxyz/xxxxx" | sed 's!\(.*\)://\(.*\)/\(.*\)!\1 \2 \3!'
    aaa abcxyz xxxxx
    in fact, if using bash, no need to call external commands
    Code:
    # string="aaa://abcxyz/xxxxx"
    # IFS="/"
    # set -- $string
    # echo $1
    aaa:
    # echo $3
    abcxyz
    # echo $4
    xxxxx

Posting Permissions

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