-1

i have w wordlist of subdomains contains like this :

admin.bugbountytarget.com
portal.bugbountytarget.com
sales.bugbountytarget.com
vpn1.bugbountytarget.com
dev.test.bugbountytarget.com
...

And I want to grep subdomains names without ".bugbountytarget.com" Only:

admin
portal
sales
...

Thank you.

3
  • 1
    This is a question on using grep, not on information security.
    – Chenmunka
    Commented Feb 10, 2023 at 18:15
  • sed -e 's/.bugbountytarget.com//g' domainlist.txt
    – ThoriumBR
    Commented Feb 10, 2023 at 18:16
  • @ThoriumBR's regex needs a trailing $ or else it'll convert oops.bugbountytarget.com.au into oops.au. It'll also show all non-matching entries without modification, which I assume is undesirable.
    – Adam Katz
    Commented Feb 10, 2023 at 19:08

1 Answer 1

2

With grep specifically (assuming it is compiled with libpcre):

grep -Po '^.*\b(?=\.bugbountytarget\.com$)' domainlist.txt

This just looks for the target at the end, in a forward lookahead (not a part of the match) and then prints the match.

With awk, no regular expressions needed (faster but you'll only notice with a massive file):

awk 'index($0, ".bugbountytarget.com") == length($0) - 19 {
  print substr($0, 1, length($0) - 20)
}' domainlist.txt

This looks for a substring (the target domain), ensures it's at the end of the line, then prints it without the target. Lengths matter.

With sed:

sed '/\.bugbountytarget\.com$/!d; s///' domainlist.txt

This finds lines that end with a literal .bugbountytarget.com and then it substitutes that last match with an empty string and prints that out.

2
  • Or sed -n 's/\.bugbountytarget\.com$//p' file which is arguably idiomatic for this use Commented Feb 17, 2023 at 1:16
  • @dave_thompson_085 – Neat! My version is theoretically faster, based on some optimization I heard a looong time ago suggesting that s/// is more expensive than //.
    – Adam Katz
    Commented Feb 17, 2023 at 17:28

Not the answer you're looking for? Browse other questions tagged .