结果
IP后面的记录省略了:比如这下面的是一行记录:
06:38:05 221.6.33.68 GET Mac=00-0A-EB-51-B2-32&DistributorId=100138&RequestType=1&RequestFlag=0&Behavior=http%3a%2f%2fwww.baidu.com%2fbaidu%3fwd%3d%25b8%25e8%25ca%25d6%25c1%25d0%25b1%25ed%26cl%3d3%26tn%3djilydg 200 RequestAD
IP后面的记录省略了:比如这下面的是一行记录:
06:38:05 221.6.33.68 GET Mac=00-0A-EB-51-B2-32&DistributorId=100138&RequestType=1&RequestFlag=0&Behavior=http%3a%2f%2fwww.baidu.com%2fbaidu%3fwd%3d%25b8%25e8%25ca%25d6%25c1%25d0%25b1%25ed%26cl%3d3%26tn%3djilydg 200 RequestAD
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace FiterIP
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button strstr;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.strstr = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// strstr
//
this.strstr.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.strstr.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.strstr.CausesValidation = false;
this.strstr.ForeColor = System.Drawing.SystemColors.Desktop;
this.strstr.Location = new System.Drawing.Point(120, 232);
this.strstr.Name = "strstr";
this.strstr.Size = new System.Drawing.Size(144, 23);
this.strstr.TabIndex = 0;
this.strstr.Text = "分析IP";
this.strstr.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 333);
this.Controls.Add(this.strstr);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
try
{
StreamReader sr = new StreamReader("E:\\problem\\leo.log",
System.Text.Encoding.GetEncoding("GB2312"));
StreamWriter sw = new StreamWriter("E:\\problem\\newleo.log",false,
System.Text.Encoding.GetEncoding("GB2312"));
String line;
int lineNum = 0;
// Read and display lines from the file until the end of
// the file is reached.
string Time=null;
string IP=null;
string temp=null; string strTime;
string strIP;
string strtemp; while ((line = sr.ReadLine())!= null)
{
string strtemp1,strtemp2,strtemp3;
string [] split =line.Split(new char[] {' '});
strtemp1 = split[0].Trim();
strtemp2 = split[1].Trim();
strtemp3 = split[2].Trim();
line = line.Trim();
lineNum++;
if(IP == null)
{
Time = strtemp1;
IP = strtemp2;
temp = strtemp3;
}
strTime = strtemp1;
strIP = strtemp2;
strtemp = strtemp3;
if( (IP)==(IP+1) )
{
sw.Write(Time+" ");
sw.Write(IP+" ");
sw.WriteLine(temp);
}
//如果IP不相同,就输入一空行!
else
{
sw.WriteLine();
}
Time = strTime;
IP = strIP;
temp = strtemp;
}
sr.Close();
sw.Close();
MessageBox.Show("Finish");
}
catch (Exception e0)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e0.StackTrace);
}
/*
finally
{
sr.Close();
sw.Close();
}
*/
}
private void Form1_Load(object sender, System.EventArgs e)
{
} }
}
这是我自个写的代码,不过不能实现,我组长看了,说我的思想不对。叫我用ArrayList写。
可我对Arraylist不懂。没用过。急,在线等!
不知你要说什么,
你可以定义一个类,
public class IPInfo:System.IComparable
{
public string time
{
get{}set{}
]
public string ipget{}set{}
]}
你可以把你得到的字符串放到一个数组内.
参照
using System;namespace webUserWindowExample.test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class KeyAndValue:System.IComparable
{
private int _key;
private string _value; public int Key
{
get{return this._key;}
} public string Value
{
get{return this._value ;}
}
public KeyAndValue(int key,string svalue)
{
_key=key;
_value=svalue;
}
public int CompareTo(object obj)
{
if(obj is KeyAndValue)
{
KeyAndValue temp = (KeyAndValue) obj; return _key.CompareTo(temp.Key );
}
throw new ArgumentException("object is not a Temperature");
}
}
}在webform1 page_load()中 webUserWindowExample.test.KeyAndValue [] a=new webUserWindowExample.test.KeyAndValue[5];
a[0]=new webUserWindowExample.test.KeyAndValue(30,"aaa");
a[1]=new webUserWindowExample.test.KeyAndValue(28,"bbb");
a[2]=new webUserWindowExample.test.KeyAndValue(37,"ccc");
a[3]=new webUserWindowExample.test.KeyAndValue(23,"ddd");
a[4]=new webUserWindowExample.test.KeyAndValue(35,"eee"); Array.Sort(a); foreach(webUserWindowExample.test.KeyAndValue kv in a)
{
Response.Write(kv.Value);
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;namespace AnalyIP
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private ArrayList al = new ArrayList();
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(8, 80);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(272, 184);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(184, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(216, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "打开文件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(168, 48);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "分析IP";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.richTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void AnalyIP(string str)
{
string[] st = str.Split(' ');
string a = st[0] + " " + st[1];
this.al.Add(a);
al.Sort();
}
private void AnalyTxt(string path)
{
string str = null;
StreamReader sr = new StreamReader(path);
while((str = sr.ReadLine()) != null)
{
this.AnalyIP(str);
}
sr.Close();
} private void button1_Click(object sender, System.EventArgs e)
{
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName;
}
} private void button2_Click(object sender, System.EventArgs e)
{
this.AnalyTxt(this.textBox1.Text);
for(int i=0;i<al.Count;i++)
{
this.richTextBox1.Text += al[i].ToString() + "\r\n";
}
}
}
}
using System.Text.RegularExpression;private string ExtractedIPAddress( string text )
{
Regex r = new Regex(@"\d{2}:\d{2}:\d{2}\s+(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+GET");
Match m = r.Match(textBox1.Text);ArrayList al = new ArrayList();
while (m.Success)
{
string ip = m.Groups["IP"].Value;
al.Add( ip );
m = m.NextMatch();
}StringBuilder sb = new StringBuilder();
while ( al.Count > 0 )
{
string ip = al[ 0 ].ToString() ;
al.RemoveAt( 0 );
sb.Append( ip + Environment.NewLine );
bool blankLine = al.IndexOf( ip ) > 0 ? true : false;while ( al.IndexOf( ip ) > -1 )
{
int index = al.IndexOf( ip );
al.RemoveAt( index );
sb.Append( ip + Environment.NewLine );
}if ( blankLine )
{
sb.Append( Environment.NewLine );
}
}return sb.ToString();
}
majianbing() ( ) 的代码是个死循环。郁闷。也没分析出来 [email protected]
帮我的加我MSN吧。
using System.Text.RegularExpression;private string ExtractedIPAddress( string text )
{
Regex r = new Regex(@"\d{2}:\d{2}:\d{2}\s+(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+GET"); //使用正则表达式提取IP地址
Match m = r.Match(textBox1.Text);ArrayList al = new ArrayList();
while (m.Success) //将所有的IP地址存入ArrayList
{
string ip = m.Groups["IP"].Value;
al.Add( ip );
m = m.NextMatch();
}StringBuilder sb = new StringBuilder();
while ( al.Count > 0 ) //每循环一次,从ArrayList中去掉第一个IP,直到个数为0
{
string ip = al[ 0 ].ToString() ; //获得ArrayList中的第一个IP地址
al.RemoveAt( 0 ); //将ArrayList第一个子项移除
sb.Append( ip + Environment.NewLine ); //写入字符串中
bool blankLine = al.IndexOf( ip ) > 0 ? true : false; //如果ArrayList中还有这个IP,则设一个空白行while ( al.IndexOf( ip ) > -1 ) //循环将这个IP读出放在一起,并从ArrayList中移去它们
{
int index = al.IndexOf( ip ); //获得ArrayList中含有相同IP的位置
al.RemoveAt( index ); //移去这个子项
sb.Append( ip + Environment.NewLine ); //加入到字符串中
}if ( blankLine ) //假如含有空白行
{
sb.Append( Environment.NewLine ); //来个回车换行啊
}
}return sb.ToString(); //返回值为字符串
}
------------------------------
输出结果:
06:38:05 218.72.34.9
06:38:05 218.72.34.906:38:05 219.145.66.226
06:38:05 218.57.171.243
06:38:05 221.6.33.68
06:38:05 221.6.33.6806:38:05 222.38.24.78
06:38:05 211.98.18.93
06:38:05 218.16.143.197
06:38:05 218.91.167.177
--------------------------------------------------------
不明白的就是为什么调试不通过??