shellawkgrep

Extract multiple parts from a URL using shell


I have a URL like this: bbc:osdb://://user=&pass=abc%sec=true

I want to extract , , ,

Example:

bbc:osdb://293.23.234.55:1234/bbc-dt-af/user=john&pass=pass123abc%sec=true

Should return:

293.23.234.55:1234:john:pass123abc

grep or egrep or sed or awk

This one:

 grep -Eo '([0-9]+.[0-9]+.[0-9]+.[0-9]+)|[0-9]{4}'

gives me first two, and in two different lines...

293.23.234.55
1234

Solution

  • This may be what you want, using any awk:

    $ awk -F'[/=&%]' -v OFS=':' '{print $3, $6, $8}' file
    293.23.234.55:1234:john:pass123abc
    

    or any sed:

    $ sed 's#.*//\([^/]*\)[^=]*=\([^&]*\)&[^=]*=\([^%]*\).*#\1:\2:\3#' file
    293.23.234.55:1234:john:pass123abc
    

    but it's hard to tell given just 1 sample input string.