Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

vi / Reg ex help

  • 09-07-2013 12:13pm
    #1
    Closed Accounts Posts: 18,966 ✭✭✭✭


    I have a file with a load of URLS in it, and I want just the IP address from the URL

    For example it looks like this:

    http://118.866.665.46:3365/tnoex')

    I can strip off the http:// with %s/http;\/\///g which gives me:
    118.866.665.46:3365/tnoex')

    however, I also want to strip off the colon, the port number and everything else to the end of the line to get:
    118.866.665.46

    The logic is match the colon and everything to the end of line and replace with nothing, I'm just not getting it and im under a deadline.

    I'm not picky on how you do it, it can be vi, awk, sed, perl.

    Any help would be greatly appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    perl -npi.bak -e 's/http:\/\/([^:\/]+).+$/$1/'
    


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    perl -npi.bak -e 's/http:\/\/([^:\/]+).+$/$1/'
    

    Thanking you!


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    or in bash shell script... though i cannot seem to do it in 1 command :/
    #!/bin/bash
    
    mUrl="http://118.866.665.46:3365/tnoex')"
    step1=${mUrl#http://*}
    echo "IP: " ${step1%:*}
    

    the first pattern-matching operator should remove, from the start, everything matching "http://*" to set step1 equal
    118.866.665.46:3365/tnoex')
    The second step will remove, from the end, everything matching ":*" leaving the IP

    These are old korn shell operators but they seem to work still in bash.

    Pity I cannot combined them. It's been a very long time since I really wrote scripts so I cannot remember if it is actually possible to combine these or not!
    Anyone else remember?


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    Thanks croo.

    We really should do a Boards - Unix beers, with me buying the first round.

    Might be an idea for a thread. Post your reg ex recipes.


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    syklops wrote: »
    Thanks croo.

    We really should do a Boards - Unix beers, with me buying the first round.

    Might be an idea for a thread. Post your reg ex recipes.

    Beer and regex, 2 of my favourite things, see who can call out a regex to match a given subset of strings after a few beers, not everybody's idea of a party game I grant you ;)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    For those who share my bizarre interest in regex, an interesting site...

    http://regexcrossword.com/


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    For those who share my bizarre interest in regex, an interesting site...

    http://regexcrossword.com/

    Any hints for symbolism on beginner level?


  • Registered Users, Registered Users 2 Posts: 5,238 ✭✭✭humbert


    PrzemoF wrote: »
    Any hints for symbolism on beginner level?
    It's much easier than it looks. ? means optional (had to look that one up, thought it meant lazy matching) and / has no meaning, again iirc.


  • Registered Users, Registered Users 2 Posts: 20,195 ✭✭✭✭jimgoose


    croo wrote: »
    or in bash shell script... though i cannot seem to do it in 1 command :/...
    echo "http://118.866.665.46:3365/tnoex" | sed -e 's%^.*://%%' -e 's%:.*%%'
    


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    jimgoose wrote: »
    echo "http://118.866.665.46:3365/tnoex" | sed -e 's%^.*://%%' -e 's%:.*%%'
    

    Thanks, but that is using sed ... which is not really part of the shell script, is it?
    I was wondering was... is there a way of combining the shell pattern-matching operators? The regex built into the shell is quite powerful so I thought there must be a way.

    But yeah, sed [& awk] are incredibly powerful tools okay.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 20,195 ✭✭✭✭jimgoose


    croo wrote: »
    Thanks, but that is using sed ... which is not really part of the shell script, is it?
    I was wondering was... is there a way of combining the shell pattern-matching operators? The regex built into the shell is quite powerful so I thought there must be a way.

    But yeah, sed [& awk] are incredibly powerful tools okay.

    They're part of the shell script, but not part of the shell. Yeah, those old Korn operators seem to expect an actual shell variable in there. I tried one or two atrocities based on command substitution, but it was having none of it. ;)


Advertisement