Saturday, April 23, 2022

[SOLVED] Remove the main domain from a list of subdomains

Issue

How to remove the main domain from a list of subdomains for example if i have a file contains

www.example.com
sub.examle.com
sub2.example.com
examle.com.ar
example.com

I want to extract only

sub.example.com
sub2.example.com

I try grep -v '^www.example.com' file.txt how can i get sub domains


Solution

  • First use Perl regular expression by using the P option.
  • ^(?!www\.) get all the strings that do not begin with a www. ((?!www\.) negative lookahead).
  • \w+\. matches any number of word characters (equal to [a-zA-Z0-9_]) followed by a period.
  • \w{4,} match more than 4 word characters.
grep -P '^(?!www\.)\w+\.\w{4,}' file.txt
#> sub.examle.com
#> sub2.example.com


Answered By - Abdessabour Mtk
Answer Checked By - David Goodson (WPSolving Volunteer)