Linux Bash/Sed中提取域名与端口的正则表达式需求
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:
-Eenables extended regular expressions (no need to escape parentheses).*-epool (.*:\/\/)?matches everything up to and including the-epoolflag plus an optionalhttp:///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)\2refers 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-epooland optional prefix are present, but doesn't include them in the output[^[:space:]]+captures the domain:port, and-omakes 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




