You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何整理文本文件中的IP、Domain和URL并分类展示?

分类整理IP、域名和URL的方法

方法一:使用命令行工具(grep)

假设原始文件名为raw_list.txt,通过以下命令快速完成分类:

  1. 初始化整理文件并写入IP分类:
echo "IP" > sorted_list.txt
grep -E "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" raw_list.txt >> sorted_list.txt
  1. 追加DOMAIN分类(排除IP和URL):
echo -e "\nDOMAIN" >> sorted_list.txt
grep -vE "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" raw_list.txt | grep -vE "^https?://" >> sorted_list.txt
  1. 追加URL分类:
echo -e "\nURL" >> sorted_list.txt
grep -E "^https?://" raw_list.txt >> sorted_list.txt

执行完成后,sorted_list.txt就是分类好的结果。

方法二:使用Python脚本

如果需要更灵活的处理逻辑(比如兼容IPv6、特殊域名格式),可以用Python脚本实现:

创建sort_ip_domain_url.py文件,内容如下:

import re

# 定义匹配规则
ip_regex = re.compile(r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
url_regex = re.compile(r'^https?://')

# 读取原始数据
with open('raw_list.txt', 'r', encoding='utf-8') as infile:
    content = [line.strip() for line in infile if line.strip()]

# 分类存储
ips, domains, urls = [], [], []
for item in content:
    if ip_regex.match(item):
        ips.append(item)
    elif url_regex.match(item):
        urls.append(item)
    else:
        domains.append(item)

# 写入整理结果
with open('sorted_list.txt', 'w', encoding='utf-8') as outfile:
    outfile.write("IP\n")
    outfile.write('\n'.join(ips) + '\n\n')
    outfile.write("DOMAIN\n")
    outfile.write('\n'.join(domains) + '\n\n')
    outfile.write("URL\n")
    outfile.write('\n'.join(urls))

使用步骤:

  1. 将原始内容保存为raw_list.txt
  2. 运行脚本:python sort_ip_domain_url.py
  3. 分类结果会自动生成到sorted_list.txt

注意事项:

  • 上述IP正则仅匹配IPv4,若需支持IPv6,需替换为对应的正则表达式
  • 域名分类默认是排除IP和URL后的剩余项,若有带端口号、特殊后缀的域名,可调整判断逻辑

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 03:25:17