Filtering specific numbers from a .txt file with C#

query filter c#
openfiledialog filter c#
filtering algorithms c#
openfiledialog filter txt
c# open file dialog filter multiple extensions
c openfiledialog filter image files
c# filter object
sorting strings in c#

I have been trying to mess around with the AutoCAD LT command line in the hopes of linking it with a C# program. I've focused on the command line because there are steep limitations when it comes to coding for LT releases. In my quest I figured out how to record commands into a log file (saved as a .txt file).

The log file:

Command: _dimlinear 
Specify first extension line origin or select object:
Specify second extension line origin:
Specify dimension line location or
[Mtext/Text/Angle/Horizontal/Vertical/Rotated]:
Dimension text = 712.75
Command: DIMLINEAR
Specify first extension line origin or <select object>: 
Specify second extension line origin: 
Specify dimension line location or 
[Mtext/Text/Angle/Horizontal/Vertical/Rotated]: 
Dimension text = 628.70

I am trying to only extract the numbers in the lines starting with "Dimension text = ". How do I achieve this using C#?

You can do it with Linq and Regex.

var array = str.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);
var regex = new Regex(@"(Dimension)\s+(text)\s+(=)\s+(?<Value>([+-]?(\d*\.)?\d+)+)",RegexOptions.Compiled);
var resultArray =  from entry in array
                let match = regex.Match(entry)
                where match.Success                
                select decimal.Parse(match.Groups["Value"].Value);

Output for the given example in OP

712.75 
628.70 

How to sort or filter text data by any word or field (LINQ) (C#), How to sort or filter text data by any word or field (LINQ) (C#) The field may be dynamically specified at runtime. Assume that the fields in scores.csv represent a student's ID number, followed by a series of four test scores. Copy the scores.​csv data from the topic How to join content from dissimilar files  I have been trying to mess around with the AutoCAD LT command line in the hopes of linking it with a C# program. I've focused on the command line because there are steep limitations when it comes to coding for LT releases. In my quest I figured out how to record commands into a log file (saved as a .txt file). The log file:

        FileInfo fi = new FileInfo("FILE LOCATION");
        StreamReader reader = new StreamReader( fi.ToString() );
        String Line = "";
        while ( ( Line = reader.ReadLine() ) != null )
        {
            if ( !Line.Contains("Dimension text") {
                continue;
            }
            String Property = Line.Split( '=' )[0];
            String Value = Line.Substring( Property.Length + 1, Line.Length - Property.Length - 1 );
            Console.WriteLine( Value ); // This line will print value of Dimension text
        }

hope this will work for you.

FileDialog.Filter Property (System.Windows.Forms), The strings for different filtering options are separated by the vertical bar. The following is an example of a filter string: Text files (*.txt)|*.txt|All files (*.*)|*  Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim myStream As Stream = Nothing Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.InitialDirectory = "c:\" openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" openFileDialog1.FilterIndex = 2 openFileDialog1.RestoreDirectory = True If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Try myStream = openFileDialog1.OpenFile() If (myStream IsNot Nothing) Then

You can use File.ReadLines and LINQ to query the file and split the lines you're interested in, to select the dimensions. I'm not sure how it'll perform if the file is huge, but it should work pretty well.

var logFile = File.ReadLines("your_file_path")

var dimensions = (from entry in logFile
                  where entry.StartsWith("Dimension text")
                  let parts = entry.Split(' ')
                  select decimal.Parse(parts.Last())).ToList();

Filtering Data (C#), Filtering refers to the operation of restricting the result set to contain only those elements that satisfy a specified condition. It is also known as  How to query for files with a specified attribute or name (C#) 07/20/2015; 2 minutes to read +6; In this article. This example shows how to find all files that have a specified file name extension (for example ".txt") in a specified directory tree. It also shows how to return either the newest or oldest file in the tree based on the creation

You could use Regular Expressions.

var input = @"Command: _dimlinear 
Specify first extension line origin or select object:
Specify second extension line origin:
Specify dimension line location or
[Mtext/Text/Angle/Horizontal/Vertical/Rotated]:
Dimension text = 712.75
Command: DIMLINEAR
Specify first extension line origin or <select object>: 
Specify second extension line origin: 
Specify dimension line location or 
[Mtext/Text/Angle/Horizontal/Vertical/Rotated]: 
Dimension text = 628.70
Dimension text = 628";
            var regex = new Regex( @"Dimension text = (\d+(\.\d*)?)" );
            foreach( Match match in regex.Matches( input ) ) {
                var num = decimal.Parse( match.Groups[ 1 ].Value );
                Console.WriteLine( "Num: " + num );
            }

How to search strings (C# Guide), Methods of the String class search for specific text. String class, provides a number of useful methods for searching the contents of a string. You can pass the path of a text file to the StreamReader constructor to open the file automatically. The ReadLine method reads each line of text, and increments the file pointer to the next line as it reads. When the ReadLine method reaches the end of the file, it returns a null reference. Create a sample text file in Notepad.

I made a tiny change to the answer posted by @AnuViswan, to get every entry:

string str = richTextBox1.Text;
        var array = str.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        var regex = new Regex(@"(?<Value>([+-]?(\d*\.)?\d+)+)", RegexOptions.Compiled);
        MatchCollection matches = regex.Matches(str);
        foreach (Match la in matches)
        {
            Console.writeLine(la);
        }

[Solved] How to search for numbers in txt file C# ?, This reply assumes you have successfully read the data into a List<List<int>> named 'data'. Hide Expand Copy Code. private List<List<int>>  I work with C++, not C# but its a simple matter to see whats wrong by debugging the code. The changes are trivial and I won't mention them here; just the working code is pasted here. To separate lines in the output I tried the C escape sequence "\r" and " " but I guess C# uses some different method.

Beginning Visual C#® 2005, A simple filter string can look like this: Text Documents (*.txt)|*.txt|All Files|*. Figure 16-3 A filter has multiple segments separated with the pipe character (|). Two strings are required for each entry, so the number of segments should always be  How to query the contents of text files in a folder (LINQ) (C#) 07/20/2015; 2 minutes to read +7; In this article. This example shows how to query over all the files in a specified directory tree, open each file, and inspect its contents. This type of technique could be used to create indexes or reverse indexes of the contents of a directory tree.

Extract numbers from a text file and add them using Python , Count number of lines in a text file in Python · Python | Count occurrences of each word in given text file (Using dictionary) · Python program to Reverse a single  Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".

Exclude or filter files using file type and regular expressions (regex , Certain files and file types can be globally excluded from a backup without having to modify the file selection by unchecking each individual  C# Method for Reading Specific Line of Text File. The above example works fine for simple C# projects, but suppose you want to use the code to read specific lines in multiple text files throughout your project. It would be helpful to create a method that receives as parameters a file path and a line number to read.

Comments
  • Please try it first and then when you are stuck you can find help here.