求文本中每个URL出现的个数-0804

import java.util.*; 
import java.io.*; 


public class ProURL{ 
private HashMap map; 

public ProURL(){ 
this.map=new HashMap(); 
} 

private String parseURL(String fPath){ 
String rFile="rurl.txt"; 
try{ 
FileReader fr=new FileReader(fPath); 
BufferedReader br=new BufferedReader(fr); 
FileWriter fw=new FileWriter(rFile); 
BufferedWriter bw=new BufferedWriter(fw); 
String str; 
while((str=br.readLine())!=null){ 
StringBuffer tmp; 
String pre; 
String[] array=str.split("/"); 
int len=array.length; 
pre=array[0]+"//"; 
try{ 
for(int i=2;i<len-1;i++){ 
tmp.append(array[i]); 
String strTmp=tmp.toString(); 
strTmp=strTmp+"\n"; 
bw.write(strTmp); 
pre=strTmp; 

} 
bw.flush(); 
bw.close(); 
fw.close(); 
}catch(Exception ex){ 
} 

} 

}catch(Exception ex){ 
ex.printStackTrace(); 
} 

return rFile; 

} 
private void addURL(String url){ 

if(map.containsKey(url)){ 
int c=((Integer)map.get(url)).intValue()+1; 
map.put(url,new Integer(c)); 
}else{ 
map.put(url,1); 
} 

} 

public void process(String fPath){ 
String rPath=parseURL(fPath); 
FileReader fr=new FileReader(rPath); 
BufferedReader br=new BufferedReader(fr); 
String url; 
while((url=br.readLine())!=null){ 
this.addURL(url); 
} 

fr.close(); 
br.close(); 
} 

public void main(String[] args){ 
ProURL pro=new ProURL(); 
String fPath="url.txt"; 
pro.process(fPath); 

} 

} 

 

 

 

shell命令:

awk -F/ '{pre=$1"//";i=3; while(i<NF){str=pre$i"/"; count[str]++;i++;}} END{for(url in count){ print url,count[url];}}' urlfile

 

看来shell还是强大的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值