I have a list of n number (n is known) of URLs. How can I remove the protocol name from the URL?

Eg : from https://xyz.com, I want to fetch xyz.com part only.


Using GNU grep and PCRE matching,

grep -oP 'https://\K\S+' file

That outputs the non-spaces following "https://"

  • Thank you it worked :) – DedSec Aug 26 '20 at 17:55
  • Yes this will also work :) – DedSec Aug 26 '20 at 18:04
  • If you want to do that, use '(http|https)://...' -- [http,https]://... means "an h or a t or a p or an s or a comma followed by colon slash slash. More succinctly, use https?://... which means "http followed optionally by an s followed by colon slash slash" – glenn jackman Aug 26 '20 at 18:12
  • Sorry about that! The (http|https)://... actually works better where I tested the [ ] and it returned all with h, t or p in them regardless of where they were in the beginning of the line. It was giving me a false positive because that was all I had in my file. I stand corrected. =) – Terrance Aug 27 '20 at 14:02

How about using awk with F option and text redirection?

awk -F'^http[s]?://' '{print $2}' fileName > outputFile

Or sed with i option

sed -i s/'http[s]\?:\/\/'// fileName

Here is a simple awk invocation:

$ awk -F// '{print $NF}' fileName

-F specifies the field separator. $NF prints the last field.

You might need to use double quotes around //.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.