0

shell command using sed editor

echo http://www.domain.com/?params | sed -e 's;https\?://;;' | sed -e 's;/.*$;;'

output www.domain.com

How can i use the unix awk editor to output this string?

awk {print} ?

1
  • Do you REALLY need awk, as opposed to cut or some other tool? If you post a FEW lines of representative input, including any you think will be hard to parse, and expected output you might get a better answer than if you don't. Commented Sep 3, 2014 at 15:58

4 Answers 4

5

You could try this,

$ echo http://www.domain.com/?params | awk -F/ '{print $3}'
www.domain.com

Through grep,

$ echo http://www.domain.com/?params | grep -oP '(?<=/)[^/]+(?=/)'
www.domain.com

Through sed,

$ echo http://www.domain.com/?params | sed 's/.*\/\([^\/]\+\)\/.*/\1/'
www.domain.com
Sign up to request clarification or add additional context in comments.

Comments

2

In addition to all the perl/sed/awk/grep, this is actually pretty well-suited for cut:

$ echo http://www.domain.com/?params | cut -d/ -f3
www.domain.com

Comments

0

With awk and gsub:

$ echo http://www.domain.com/?params | awk '{gsub("\\/\\?params$", ""); print}'
http://www.domain.com
$ echo http://www.domain.com/?params | awk '{gsub("\\/\\?params$",""); gsub("^http://",""); print}'
www.domain.com

Or, (thanks Avinash Raj):

$ echo http://www.domain.com/?params | awk '{gsub(/\/\?params$|^http:\/\//,""); print }' 
www.domain.com

Perl:

$ echo http://www.domain.com/?params | perl -pe 's/^http:\/\/([^\/]+).*/\1/' 
www.domain.com

Or:

$ echo http://www.domain.com/?params | perl -ne 'print "$1\n" if /^http:\/\/([^\/]+)/; ' 
www.domain.com

1 Comment

single gsub will do this job $ echo http://www.domain.com/?params | awk '{gsub(/\/\?params|http:\/\//,"")}1'
0

Mix it up a bit

expr match http://www.domain.com/?params '.*/\(.*\)/'

or

expr http://www.domain.com/?params : '.*/\(.*\)/'

Incase there are more slashes later on

expr http://www.domain.com/?params/ : '.*//\([^/]*\)/'

Incase something really wierd is going on with the slashes

expr http://www.domain.com/?params/ : '[^/]*//\([^/]*\)/'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.