Skip to main content
ubuntuask.com

Back to all posts

How to Extract Part Of A URL In Bash Using Regex?

Published on
5 min read
How to Extract Part Of A URL In Bash Using Regex? image

Best URL Extraction Scripts to Buy in November 2025

1 Pimple Popper Tool Kit Blackhead Remover Tools (5 Piece Kit), Professional Stainless Black Head Remover for face, Risk Free Treatment Pimple Extractor with Tweezers

Pimple Popper Tool Kit Blackhead Remover Tools (5 Piece Kit), Professional Stainless Black Head Remover for face, Risk Free Treatment Pimple Extractor with Tweezers

  • SAFE, STERILIZED TOOLS: PREMIUM MATERIALS ENSURE RUST-FREE, SAFE USE.

  • VERSATILE 9-IN-1 DESIGN: EFFORTLESSLY TACKLE ALL BLEMISH TYPES AT HOME.

  • ERGONOMIC GRIP: NON-SLIP HANDLE PROVIDES PRECISION AND CONTROL FOR OPTIMAL RESULTS.

BUY & SAVE
$5.99
Pimple Popper Tool Kit Blackhead Remover Tools (5 Piece Kit), Professional Stainless Black Head Remover for face, Risk Free Treatment Pimple Extractor with Tweezers
2 Teenitor Pimple Extractor Acne Treatment Tool Surgical Grade Blackhead Comedone Removal 2-in-1 Popper Tool for Face Nose Blackhead Extractor Tool Silver

Teenitor Pimple Extractor Acne Treatment Tool Surgical Grade Blackhead Comedone Removal 2-in-1 Popper Tool for Face Nose Blackhead Extractor Tool Silver

  • STERILIZED STAINLESS STEEL: SAFE, RUST-FREE, AND INFECTION-FREE USE.

  • VERSATILE 4-IN-1 TOOL: EFFECTIVELY REMOVES ALL TYPES OF BLEMISHES.

  • PROFESSIONAL RESULTS AT HOME: ENJOY CLEAR SKIN WITHOUT DERMATOLOGIST VISITS.

BUY & SAVE
$3.99 $4.39
Save 9%
Teenitor Pimple Extractor Acne Treatment Tool Surgical Grade Blackhead Comedone Removal 2-in-1 Popper Tool for Face Nose Blackhead Extractor Tool Silver
3 Teenitor Pimple Popper Tool Kit and Blackhead Remover Tools, Ingrown Hair Tweezers, Professional Blackhead Extractor Pimple Extractor Comedone Extractor, Black Head Remover for Face

Teenitor Pimple Popper Tool Kit and Blackhead Remover Tools, Ingrown Hair Tweezers, Professional Blackhead Extractor Pimple Extractor Comedone Extractor, Black Head Remover for Face

  • PRECISION TOOLS FOR PAINLESS BLACKHEAD AND PIMPLE REMOVAL.
  • STERILIZABLE STAINLESS STEEL ENSURES SAFE AND HYGIENIC USE.
  • ERGONOMIC DESIGN OFFERS BETTER CONTROL FOR HARD-TO-REACH AREAS.
BUY & SAVE
$6.99 $7.99
Save 13%
Teenitor Pimple Popper Tool Kit and Blackhead Remover Tools, Ingrown Hair Tweezers, Professional Blackhead Extractor Pimple Extractor Comedone Extractor, Black Head Remover for Face
4 Boxoyx Pimple Popper Tool Kit - 6 Pcs Blackhead Remover Comedone Extractor Tool Kit with Metal Case for Quick and Easy Removal of Pimples, Blackheads, Zit Removing, Forehead, Facial and Nose(Silver)

Boxoyx Pimple Popper Tool Kit - 6 Pcs Blackhead Remover Comedone Extractor Tool Kit with Metal Case for Quick and Easy Removal of Pimples, Blackheads, Zit Removing, Forehead, Facial and Nose(Silver)

  • ACHIEVE PROFESSIONAL RESULTS AT HOME, BOOSTING YOUR CONFIDENCE EASILY.
  • SAFE, DURABLE STAINLESS STEEL TOOLS ENSURE RELIABILITY AND MINIMAL SCARRING.
  • ERGONOMIC, NON-SLIP DESIGN OFFERS PRECISION CONTROL DURING USE.
BUY & SAVE
$9.99
Boxoyx Pimple Popper Tool Kit - 6 Pcs Blackhead Remover Comedone Extractor Tool Kit with Metal Case for Quick and Easy Removal of Pimples, Blackheads, Zit Removing, Forehead, Facial and Nose(Silver)
5 Professional Blackhead Remover Blemish Extractor Tool - Pimple Comedone Removal 2-in-1 Stainless Steel Pimple Popper(2pcs)

Professional Blackhead Remover Blemish Extractor Tool - Pimple Comedone Removal 2-in-1 Stainless Steel Pimple Popper(2pcs)

BUY & SAVE
$5.89
Professional Blackhead Remover Blemish Extractor Tool - Pimple Comedone Removal 2-in-1 Stainless Steel Pimple Popper(2pcs)
6 Suvorna Pimple Popper Tool Kit | Milia Remover | Lancets for Facial Extraction | White head Extractor Tool for Face | Comedone Extractor | Blackhead Remover tool | Acne Needle Tool & Cyst Removal Tool

Suvorna Pimple Popper Tool Kit | Milia Remover | Lancets for Facial Extraction | White head Extractor Tool for Face | Comedone Extractor | Blackhead Remover tool | Acne Needle Tool & Cyst Removal Tool

  • FRENCH STAINLESS STEEL ENSURES SAFETY AND INFECTION PREVENTION.
  • DIVERSE TOOLS FOR EFFECTIVE MILIA, BLACKHEAD, AND PIMPLE EXTRACTION.
  • STYLISH POUCH KEEPS TOOLS ORGANIZED AND EASILY PORTABLE.
BUY & SAVE
$13.99
Suvorna Pimple Popper Tool Kit | Milia Remover | Lancets for Facial Extraction | White head Extractor Tool for Face | Comedone Extractor | Blackhead Remover tool | Acne Needle Tool & Cyst Removal Tool
7 Pointed Blackhead Removal, 2-in-1 Acne Extractor Tool - Stainless Steel Whitehead Remover for Face/Nose

Pointed Blackhead Removal, 2-in-1 Acne Extractor Tool - Stainless Steel Whitehead Remover for Face/Nose

  • DURABLE STAINLESS STEEL: STRONG, HEAT-TREATED, AND DEFORMATION-RESISTANT.
  • RUST-RESISTANT COATING: LONG-LASTING, RELIABLE PERFORMANCE FOR SKINCARE.
  • USER-FRIENDLY DESIGN: EASY TO OPERATE FOR PAINLESS ACNE REMOVAL.
BUY & SAVE
$9.19
Pointed Blackhead Removal, 2-in-1 Acne Extractor Tool - Stainless Steel Whitehead Remover for Face/Nose
8 Alden 8440P Pro Grabit Broken Bolt and Damaged Screw Extractor 4 Piece Kit - Made in The USA

Alden 8440P Pro Grabit Broken Bolt and Damaged Screw Extractor 4 Piece Kit - Made in The USA

  • ALL-IN-ONE TOOL: DRILL AND EXTRACTOR, NO EXTRA TOOLS NEEDED!

  • ACCURATE SELF-CENTERING TIP PREVENTS WALKING OR WANDERING.

  • LIFETIME WARRANTY: GUARANTEED QUALITY FROM ALDEN, USA-MADE!

BUY & SAVE
$34.69 $39.99
Save 13%
Alden 8440P Pro Grabit Broken Bolt and Damaged Screw Extractor 4 Piece Kit - Made in The USA
9 TsMADDTs Blackhead Remover Tools and Ingrown Hair Tweezers, 9PCS Pimple Popper Tool Kit, Ingrown Hair Removal Kit with Pimple Extractor, Pore Extractor, Tweezers, Razor

TsMADDTs Blackhead Remover Tools and Ingrown Hair Tweezers, 9PCS Pimple Popper Tool Kit, Ingrown Hair Removal Kit with Pimple Extractor, Pore Extractor, Tweezers, Razor

  • COMPLETE 9-PIECE SET FOR SAFE, EFFECTIVE BLACKHEAD AND PIMPLE REMOVAL.

  • SURGICAL-GRADE STAINLESS STEEL ENSURES DURABILITY AND EASY CLEANING.

  • PRECISION TWEEZERS AND TOOLS FOR PERFECT BROW SHAPING AND HAIR REMOVAL.

BUY & SAVE
$8.99
TsMADDTs Blackhead Remover Tools and Ingrown Hair Tweezers, 9PCS Pimple Popper Tool Kit, Ingrown Hair Removal Kit with Pimple Extractor, Pore Extractor, Tweezers, Razor
10 MelodySusie Blackhead Remover Pimple Popper Tool Kit - Professional Extractor for Nose and Face, Stainless Comedone, Blemish Whitehead Popping with Portable Metal Case.

MelodySusie Blackhead Remover Pimple Popper Tool Kit - Professional Extractor for Nose and Face, Stainless Comedone, Blemish Whitehead Popping with Portable Metal Case.

  • ALL-IN-ONE SOLUTION: 8 TOOLS FOR EFFORTLESS BLEMISH AND BLACKHEAD REMOVAL.
  • SKIN-FRIENDLY DESIGN: MADE FROM STAINLESS STEEL, SAFE FOR ALL SKIN TYPES.
  • ERGONOMIC CONTROL: ANTI-SLIP HANDLE ENSURES PRECISION AND COMFORT IN USE.
BUY & SAVE
$6.99
MelodySusie Blackhead Remover Pimple Popper Tool Kit - Professional Extractor for Nose and Face, Stainless Comedone, Blemish Whitehead Popping with Portable Metal Case.
+
ONE MORE?

To extract part of a URL in bash using regex, you can use the grep or sed commands along with regular expressions.

Here is an example of how you can extract a specific part of a URL in bash using regex:

url="https://www.example.com/posts/1234" regex="/posts/([0-9]+)" if [[ $url =~ $regex ]]; then matched_string=${BASH_REMATCH[0]} extracted_part=${BASH_REMATCH[1]} echo "Matched string: $matched_string" echo "Extracted part: $extracted_part" else echo "No match found." fi

In this example, we have a URL stored in the url variable. The regex pattern /posts/([0-9]+) is used to match and extract the numeric part after /posts/ in the URL.

The =~ operator in the if statement checks if the URL matches the regex pattern. If it does, the matched string is stored in the ${BASH_REMATCH[0]} array element, and the extracted part is stored in ${BASH_REMATCH[1]}.

Finally, the extracted part and the matched string are printed in the output. If no match is found, it displays a message stating "No match found."

You can modify the regex pattern to match different parts of the URL based on your requirements.

What is the regex pattern to extract the path from a URL in bash?

To extract the path from a URL in bash using regular expressions, you can use the following regex pattern:

regex="/([^[:space:]/?#]*)"

Here's an example on how to use this pattern to extract the path from a URL:

url="https://www.example.com/path/to/something?param=value#fragment" if [[ $url =~ $regex ]]; then path="${BASH_REMATCH[1]}" echo "Path: $path" fi

In the above example, the extracted path will be stored in the variable $path and then printed. The output will be:

Path: /path/to/something

Note that this regex pattern assumes that the URL doesn't contain spaces, query parameters, or fragments in the path. It will match any character except /, ?, and #, which are common delimiters in URLs.

What is the best way to extract the subdomain from a URL using bash regex?

One possible way to extract the subdomain from a URL using bash regex is by using the following approach:

#!/bin/bash

url="http://subdomain.example.com/path/to/page" # Replace with your URL regex='^http[s]?://([^/]+)'

if [[ $url =~ $regex ]]; then subdomain=${BASH_REMATCH[1]} echo "Subdomain: $subdomain" else echo "No subdomain found" fi

In this example, the URL is stored in the url variable. The regex pattern ^http[s]?://([^/]+) is used to match the subdomain.

Explanation of the regex pattern:

  • ^ matches the start of the line.
  • http is the literal text "http".
  • [s]? matches an optional "s" (for "https").
  • :// matches the literal "://".
  • ([^/]+) captures one or more characters that are not a forward slash (/) into a capturing group.

If the pattern matches the URL, the captured subdomain can be accessed using ${BASH_REMATCH[1]}. The example script then prints the extracted subdomain.

If the pattern does not match the URL, it means there is no subdomain, and the script prints "No subdomain found".

Note: This solution assumes that the URLs provided are in the form http://subdomain.example.com/path/to/page. For different URL formats, the regex pattern may need to be adjusted.

What is the regex pattern to capture the TLD and subdomain from a URL in bash?

The regex pattern to capture the top-level domain (TLD) and subdomain from a URL in bash is as follows:

^(http(s)?:\/\/)?([a-zA-Z0-9_-]+\.)*([a-zA-Z0-9_-]+)\.([a-zA-Z]{2,6})(\/.*)?$

This regex pattern is divided into several parts:

  • ^(http(s)?:\/\/)? is used to match an optional "http://" or "https://", allowing for URLs with or without a protocol.
  • ([a-zA-Z0-9_-]+\.) captures any combination of alphanumeric characters, underscores, and hyphens followed by a period, representing subdomains.
  • ([a-zA-Z0-9_-]+) captures any combination of alphanumeric characters, underscores, and hyphens, representing the main domain name.
  • \.([a-zA-Z]{2,6}) captures the TLD, which can be composed of two to six alphabetical characters.
  • (\/.*)? captures any optional path or query string after the domain.

Here's an example of how you can use this pattern to extract the TLD and subdomain from a URL in bash:

url="http://www.example.com/path?page=1" if [[ $url =~ ^(http(s)?:\/\/)?([a-zA-Z0-9_-]+\.)*([a-zA-Z0-9_-]+)\.([a-zA-Z]{2,6})(\/.*)?$ ]]; then subdomain="${BASH_REMATCH[3]}" tld="${BASH_REMATCH[4]}" echo "Subdomain: $subdomain" echo "TLD: $tld" else echo "Invalid URL" fi

Running this code will output:

Subdomain: www TLD: example

How to extract the TLD (Top-Level Domain) from a URL in bash?

To extract the TLD (Top-Level Domain) from a URL in bash, you can use the following method:

url="https://www.example.com" tld=$(echo "$url" | awk -F'/' '{split($3, a, "."); print a[length(a)-1] "." a[length(a)]}') echo "$tld"

This script uses awk to split the URL by forward slash (/) and then further split the third field (which is the domain) by periods (.). It then prints the second-to-last and last elements of the resulting array, which represent the TLD.

In this example, the output will be example.com.

What is the regex pattern to capture the URL scheme (http, ftp) in bash?

To capture the URL scheme (http, ftp) using regex in bash, you can use the following pattern:

^(http|ftp)://

Explanation:

  • ^ represents the start of the line.
  • (http|ftp) is a regex group that captures either "http" or "ftp".
  • :// matches the literal characters "://", which follows the scheme in most URLs.

Here's an example using bash:

url="http://example.com" regex="^(http|ftp)://"

if [[ $url =~ $regex ]]; then scheme="${BASH_REMATCH[1]}" echo "URL scheme: $scheme" fi

This script will output:

URL scheme: http

Note that BASH_REMATCH[1] contains the captured value of the first group (http|ftp).