You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Linux Bash/Sed中提取域名与端口的正则表达式需求

Extract Domain and Port in Bash/Sed Using Regex

Got it, let's work through this to get that clean domain:port combo you need. You already nailed matching the leading -epool (.*://)? section—now we just need to either strip that off or directly grab what comes right after it. Here are a few practical, Bash/Sed-friendly methods:

Method 1: Strip Leading Unwanted Content with Sed

If your input lines look like miner -epool http://lb.geo.pirlpool.eu:8002 -ewal mywallet, you can use Sed to erase the leading matched portion entirely, leaving only the domain:port.

Use this command (extended regex makes the syntax cleaner):

sed -E 's/.*-epool (.*:\/\/)?//' your_input_file.txt

Breakdown:

  • -E enables extended regular expressions (no need to escape parentheses)
  • .*-epool (.*:\/\/)? matches everything up to and including the -epool flag plus an optional http:///https:// prefix
  • Replacing that match with an empty string leaves exactly the domain:port you're targeting.

Method 2: Directly Capture the Target with Sed (Ideal for Lines with Extra Text)

If there's extra content after the domain:port, you can explicitly capture the non-space characters that form your target and replace the whole line with just that capture:

sed -E 's/.*-epool (.*:\/\/)?([^[:space:]]+).*/\2/' your_input_file.txt

Breakdown:

  • [^[:space:]]+ matches one or more non-space characters (this is your domain:port, since it won't contain spaces)
  • \2 refers to the second captured group (the domain:port), so we replace the entire line with just that value.

Method 3: Use Grep for Direct Extraction

If you prefer Grep, its -o flag lets you output only the matched portion. GNU Grep supports positive lookbehind, which lets you confirm the leading section exists without including it in the result:

grep -Eo '(?<=-epool (.*://)?)[^[:space:]]+' your_input_file.txt

Breakdown:

  • (?<=-epool (.*://)?) is a positive lookbehind—it verifies the leading -epool and optional prefix are present, but doesn't include them in the output
  • [^[:space:]]+ captures the domain:port, and -o makes Grep print only that part.

Example Test

If your input line is:

miner -epool https://lb.geo.pirlpool.eu:8002 -eworker rig1

All three methods will output:
lb.geo.pirlpool.eu:8002

内容的提问来源于stack exchange,提问作者dclaudiud

火山引擎 最新活动