6 Replies - 3345 Views - Last Post: 17 August 2009 - 12:32 AM Rate Topic: -----

#1 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

extract specific line from text file

Post icon  Posted 07 August 2009 - 07:23 PM

Hi..

my text file look like this

------------------------------------------------------------------------------
AGILENT 3070 FIXTURE WIRING REPORT Sat Mar 07, 2009 06:43:05 PM

/var/hp3070/boards/Celestica/wbf303/fixture/wires
------------------------------------------------------------------------------

Fixture Type : Agilent SimPlate Express
Fixture Size : Bank 2
Fixture Part Number : 44200S
Top Probes Allowed : Enabled
Autofile : 28
Units : English
Wiring Method : Automatic
-----------------------------------------------------------------------------

( Pin ) [ Probe ] Length = in.

| From | To | From | To
Length|Ga|Color |(b r c )|(b r c )| X Y | X Y
------|--|------|---------------|---------------|-------|-------|-------|-------
1.0 30 Blue (2 15.00 28.0) [2 14.54 31.8] 94611 16275 88926 13049
1.0 30 Blue (2 15.00 29.0) [2 14.82 31.4] 93111 16275 89446 15049
1.0 30 Blue (2 15.00 30.0) [2 14.90 31.8] 91611 16275 88936 15569

------------------------------------------------------------------------------
AGILENT 3070 FIXTURE WIRING REPORT Sat Mar 07, 2009 06:43:05 PM

/var/hp3070/boards/Celestica/wbf303/fixture/wires
------------------------------------------------------------------------------

| From | To | From | To
Length|Ga|Color |(b r c )|(b r c )| X Y | X Y
------|--|------|---------------|---------------|-------|-------|-------|-------
6.5 30 Blue (2 22.00 42.0) [2 13.82 33.1] 73611 65275 86926 8049
6.5 30 Blue (2 21.00 18.0) [2 13.61 35.8] 109611 58275 82916 6549
7.0 30 Blue (2 22.00 17.0) [2 13.68 33.1] 111111 65275 86916 7049


I want to extract all the line which is 6.5 30 Blue (2 22.00 42.0) [2 13.82 33.1] 73611 65275 86926 8049
this kind line and write it in another file. I just want that kind of line without the headings everything. I also want the particular line number. this is my code so far

   Dim FileContents() As String = IO.File.ReadAllLines(strFileName)
		Dim sf As StreamWriter = File.CreateText("C:\match.txt")


		For y As Integer = 0 To FileContents.GetUpperBound(0)
			Dim patternStr As String = "\((\d \.)+\)"
			
			Dim regexObj As New RegularExpressions.Regex(patternStr)
			Dim matchColl As RegularExpressions.MatchCollection
			matchColl = regexObj.Matches(FileContents(y))

			If matchColl.Count > 0 Then
				sf.WriteLine("{0}", matchColl(0).Value)
			
			End If

		Next

		sf.Close()

But this code not writing anything in my file
I want to extract those lines according to value in ( ). can any one help me how can i solve this problem?

This post has been edited by kayatri: 07 August 2009 - 07:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: extract specific line from text file

#2 TheHoodProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-August 09

Re: extract specific line from text file

Posted 08 August 2009 - 02:14 AM

Use the regex coded below. I've tested it on your sample - so it should work. The value in () that you wanted are stored in the group named "value".

Note: with with this regex you need to set the option "RegexOptions.Multiline".

Dim patternStr As String = "^\b\d\.\d\b[\s\w]{4}\w+\s\((?<value>[\d\s.]{12})\)\s\[[\d\s.]{12}\][\d\s]*$"
		   
Dim regexObj As New RegularExpressions.Regex(patternStr, RegexOptions.Multiline)



Cheers B)
Was This Post Helpful? 0
  • +
  • -

#3 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: extract specific line from text file

Posted 09 August 2009 - 06:34 PM

View PostTheHoodProgrammer, on 8 Aug, 2009 - 01:14 AM, said:

Use the regex coded below. I've tested it on your sample - so it should work. The value in () that you wanted are stored in the group named "value".

Note: with with this regex you need to set the option "RegexOptions.Multiline".

Dim patternStr As String = "^\b\d\.\d\b[\s\w]{4}\w+\s\((?<value>[\d\s.]{12})\)\s\[[\d\s.]{12}\][\d\s]*$"
		   
Dim regexObj As New RegularExpressions.Regex(patternStr, RegexOptions.Multiline)



Cheers B)



i tryed ur regex pattern but it not work may be
my text file contain line like this also

AGILENT 3070 FIXTURE WIRING REPORT Sat Mar 07, 2009 06:43:05 PM

/var/hp3070/boards/Celestica/wbf303/fixture/wires
------------------------------------------------------------------------------

| From | To | From | To
Length|Ga|Color |(b r c )|(b r c )| X Y | X Y
------|--|------|---------------|---------------|-------|-------|-------|-------
7.0 28 Black (2 13.00 14.0) [2 11.94 37.7] 115611 2275 79996 -5121
7.0 28 Black (2 04.00 39.0) (2 13.00 50.0) 78111 -60725 61611 2275
7.0 28 Black (2 02.00 19.0) (2 11.00 18.0) 108111 -74725 109611 -11725
7.5 28 Black (2 03.00 19.0) (2 13.00 11.0) 108111 -67725 120111 2275
9.0 28 Black (2 09.00 19.0) (2 18.00 53.0) 108111 -25725 57111 37275
9.0 28 Black (2 08.00 19.0) (2 18.00 53.0) 108111 -32725 57111 37275
9.0 28 Black (2 13.00 25.0) (2 01.00 25.0) 99111 2275 99111 -81725
9.0 28 Black (2 13.00 26.0) (2 01.00 26.0) 97611 2275 97611 -81725
9.0 28 Black (2 01.00 49.0) (2 11.00 18.0) 63111 -81725 109611 -11725

2.0 28 Blue (2 14.00 64.0) [T1 ] 40611 9275 81504 -49757
2.0 28 Blue (2 14.00 63.0) [T2 ] 42111 9275 82504 -49757
5.5 28 Blue (2 20.00 17.0) [T3 ] 111111 51275 83504 -49757
2.0 28 Blue (2 14.00 58.0) [T4 ] 49611 9275 84504 -49757
1.5 28 Blue (2 11.00 61.0) [T5 ] 45111 -11725 85504 -49757
5.0 28 Blue (2 10.00 63.0) [T6 ] 42111 -18725 86504 -49757
1.0 28 Blue (2 19.00 48.0) [T7 ] 64611 44275 87504 -49757
1.5 28 Blue (2 20.00 35.0) [T8 ] 84111 51275 88504 -49757
1.0 28 Blue (2 19.00 36.0) [T9 ] 82611 44275 89504 -49757
4.5 28 Blue (2 11.00 64.0) [T10 ] 40611 -11725 90504 -49757


with this kind of line, can we stil use ur regex?
Was This Post Helpful? 0
  • +
  • -

#4 soltec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 09

Re: extract specific line from text file

Posted 14 August 2009 - 09:19 PM

View Postkayatri, on 7 Aug, 2009 - 06:23 PM, said:

Hi..

my text file look like this

------------------------------------------------------------------------------
AGILENT 3070 FIXTURE WIRING REPORT Sat Mar 07, 2009 06:43:05 PM

/var/hp3070/boards/Celestica/wbf303/fixture/wires
------------------------------------------------------------------------------

Fixture Type : Agilent SimPlate Express
Fixture Size : Bank 2
Fixture Part Number : 44200S
Top Probes Allowed : Enabled
Autofile : 28
Units : English
Wiring Method : Automatic
-----------------------------------------------------------------------------

( Pin ) [ Probe ] Length = in.

| From | To | From | To
Length|Ga|Color |(b r c )|(b r c )| X Y | X Y
------|--|------|---------------|---------------|-------|-------|-------|-------
1.0 30 Blue (2 15.00 28.0) [2 14.54 31.8] 94611 16275 88926 13049
1.0 30 Blue (2 15.00 29.0) [2 14.82 31.4] 93111 16275 89446 15049
1.0 30 Blue (2 15.00 30.0) [2 14.90 31.8] 91611 16275 88936 15569

------------------------------------------------------------------------------
AGILENT 3070 FIXTURE WIRING REPORT Sat Mar 07, 2009 06:43:05 PM

/var/hp3070/boards/Celestica/wbf303/fixture/wires
------------------------------------------------------------------------------

| From | To | From | To
Length|Ga|Color |(b r c )|(b r c )| X Y | X Y
------|--|------|---------------|---------------|-------|-------|-------|-------
6.5 30 Blue (2 22.00 42.0) [2 13.82 33.1] 73611 65275 86926 8049
6.5 30 Blue (2 21.00 18.0) [2 13.61 35.8] 109611 58275 82916 6549
7.0 30 Blue (2 22.00 17.0) [2 13.68 33.1] 111111 65275 86916 7049


I want to extract all the line which is 6.5 30 Blue (2 22.00 42.0) [2 13.82 33.1] 73611 65275 86926 8049
this kind line and write it in another file. I just want that kind of line without the headings everything. I also want the particular line number. this is my code so far

   Dim FileContents() As String = IO.File.ReadAllLines(strFileName)
		Dim sf As StreamWriter = File.CreateText("C:\match.txt")


		For y As Integer = 0 To FileContents.GetUpperBound(0)
			Dim patternStr As String = "\((\d \.)+\)"
			
			Dim regexObj As New RegularExpressions.Regex(patternStr)
			Dim matchColl As RegularExpressions.MatchCollection
			matchColl = regexObj.Matches(FileContents(y))

			If matchColl.Count > 0 Then
				sf.WriteLine("{0}", matchColl(0).Value)
			
			End If

		Next

		sf.Close()

But this code not writing anything in my file
I want to extract those lines according to value in ( ). can any one help me how can i solve this problem?



Look at your Imports for StreamReader StreamWriter
Was This Post Helpful? 0
  • +
  • -

#5 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: extract specific line from text file

Posted 15 August 2009 - 07:53 AM

What are the criteria for extracting a line? Must it be an exact match for what is between the "(" and ")"?

If so - then if you have the file in an array of strings as I suspect, it is simple to use .Split to split on ( and ) and grab the entry at offset 1 to do a string compare. The line number is the array index + 1

You can look at the documentation for .Split for splitting on multiple characters.
Was This Post Helpful? 0
  • +
  • -

#6 CapeCAD  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 64
  • Joined: 10-August 09

Re: extract specific line from text file

Posted 15 August 2009 - 12:27 PM

This will extract "(*)" out of your string which contains "(*)" and "[*]"

  
	strTemp = System.Text.RegularExpressions.Regex.Match(System.Text.RegularExpressions.Regex.Match(FileContents(Y), "\s+-?(\d*)\.?(\d+)(\s+)(\d+)(\s+).*\(.*\).*\[.*\].*").ToString, "\(.*\)").ToString




Then you can output it to your file in the "(*),lineNumber" format with:
  
If strTemp > "" Then
	My.Computer.FileSystem.WriteAllText("C:\match.txt", strTemp & "," & Y & vbCrLf, True, System.Text.Encoding.ASCII)
End If


Was This Post Helpful? 0
  • +
  • -

#7 TheHoodProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-August 09

Re: extract specific line from text file

Posted 17 August 2009 - 12:32 AM

7.0 28 Black (2 13.00 14.0) [2 11.94 37.7] 115611 2275 79996 -5121
7.0 28 Black (2 04.00 39.0) (2 13.00 50.0) 78111 -60725 61611 2275
7.0 28 Black (2 02.00 19.0) (2 11.00 18.0) 108111 -74725 109611 -11725
7.5 28 Black (2 03.00 19.0) (2 13.00 11.0) 108111 -67725 120111 2275
9.0 28 Black (2 09.00 19.0) (2 18.00 53.0) 108111 -25725 57111 37275
9.0 28 Black (2 08.00 19.0) (2 18.00 53.0) 108111 -32725 57111 37275
9.0 28 Black (2 13.00 25.0) (2 01.00 25.0) 99111 2275 99111 -81725
9.0 28 Black (2 13.00 26.0) (2 01.00 26.0) 97611 2275 97611 -81725
9.0 28 Black (2 01.00 49.0) (2 11.00 18.0) 63111 -81725 109611 -11725

2.0 28 Blue (2 14.00 64.0) [T1 ] 40611 9275 81504 -49757
2.0 28 Blue (2 14.00 63.0) [T2 ] 42111 9275 82504 -49757
5.5 28 Blue (2 20.00 17.0) [T3 ] 111111 51275 83504 -49757
2.0 28 Blue (2 14.00 58.0) [T4 ] 49611 9275 84504 -49757
1.5 28 Blue (2 11.00 61.0) [T5 ] 45111 -11725 85504 -49757
5.0 28 Blue (2 10.00 63.0) [T6 ] 42111 -18725 86504 -49757
1.0 28 Blue (2 19.00 48.0) [T7 ] 64611 44275 87504 -49757
1.5 28 Blue (2 20.00 35.0) [T8 ] 84111 51275 88504 -49757
1.0 28 Blue (2 19.00 36.0) [T9 ] 82611 44275 89504 -49757
4.5 28 Blue (2 11.00 64.0) [T10 ] 40611 -11725 90504 -49757

The above can be matched with
^\b\d\.\d\b[\s\w]{4}\w+\s([[(])(?<value>[\d\s.]+)[])]\s([[(])[\w\s.]*[])][\d\s-]*$


A conditional statement to match if: an open "(" closes with a ")" could be used ...But that is all up to you. I encourage you to just learn Regular Expressions. "Regular Expressions Cookbook" by Jan Goyvaerts and Steven Levithan, would do you good. Following that with "Mastering Regular Expressions" would not hurt either...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1