Results 1 to 6 of 6

Thread: string manipulation

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    string manipulation

    Hi guys.

    Anyone who can tell me how i can cut this string

    ftp://aaaaa:bbbbbb@cccccc.cc.cc/ddddddd-2.5/eeeee

    to

    aaaaa:bbbbbb@cccccc.cc.cc
    dddddddd-2.5
    eeeee

    i dont need the ftp:// part. I have tried to use cut, but i could not get the correct result.
    Because i would like to have the lines spitted onto a new line, and with cut i could only get it to
    split them to the same line.

    Thanks on advance.

  2. #2
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: string manipulation

    Using cut and tr:

    Code:
    echo ftp://aaaaa:bbbbbb@cccccc.cc.cc/ddddddd-2.5/eeeee | cut -c 7- | tr '/' '\n'
    ...hope that helps.

    Explanation:

    echo ftp[...] -just replace this with wherever you get the string from.

    cut -c 7- -cut any characters from the 7th onwards.

    tr '/' '\n' -replace any / character with a newline.
    Last edited by MG&TL; October 26th, 2012 at 09:09 AM.

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: string manipulation

    ... or sed

    Code:
    echo "ftp://aaaaa:bbbbbb@cccccc.cc.cc/ddddddd-2.5/eeeee" | sed -r 's#ftp://([^/]*)/.*#\1#'
    aaaaa:bbbbbb@cccccc.cc.cc
    Of course languages like php have explicit url parsing functions

    http://www.php.net/manual/en/function.parse-url.php

  4. #4
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: string manipulation

    Code:
    echo "ftp://aaaaa:bbbbbb@cccccc.cc.cc/ddddddd-2.5/eeeee" | awk -F"/" '{printf $3"\n"$4"\n"$5}
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  5. #5
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: string manipulation

    Thanks guys.

    Here is a lot i can use and hopefully learn from

    Kind regards.

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: string manipulation

    ... or
    Code:
    string='...'
    echo ${string/.cc\/*/.cc}
    Last edited by Vaphell; October 26th, 2012 at 11:31 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Tags for this Thread

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
  •