Thread: bash cut string

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    bash cut string

    hello all

    in my bash script I have some strings that I want to cut up to a specific substring.For example

    Code:
    www.something.com/dir1/some.php?id=1&ll
    and I only want to keep the link up to .php
    The same if instead of .php is .asp or .jsp

    how can I do that?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    116
    well,how can I use that?
    could you give me an example?
    the use of sed in order to cut the string when the substring .php is met is not an option?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I don't know much about it, but I have found this page.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by quo View Post
    well,how can I use that?
    You have to find a pattern which works for your data.

    could you give me an example?
    Code:
    $ cat test.txt
    www.something.com/dir1/some.php?id=1&ll
    www.somephp.org/php/index.html
    www.something.com/start.php
    www.php.net
    $ grep -o ".*\.php" < test.txt
    www.something.com/dir1/some.php
    www.something.com/start.php
    www.php
    "grep -o" just prints the matched part instead of the whole line where the match was found. You can redirect the output into a new file if you need to.

    Bye, Andreas

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can do it more efficiently within bash like this:
    Code:
    a='www.something.com/dir1/some.php?id=1&ll'
    a=${a%%\?*}
    echo $a
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Awk and sed bash script help
    By Annonymous in forum Linux Programming
    Replies: 19
    Last Post: 05-10-2012, 12:40 AM
  2. Can't run Bash!
    By Yarin in forum Tech Board
    Replies: 3
    Last Post: 05-11-2009, 07:25 PM
  3. What company to bash..?
    By h_howee in forum A Brief History of Cprogramming.com
    Replies: 74
    Last Post: 02-03-2008, 01:13 PM
  4. BASH scripting.
    By Kennedy in forum Tech Board
    Replies: 6
    Last Post: 05-06-2007, 06:13 PM
  5. bash scripting?
    By Draco in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2007, 06:15 AM