rulururu

post Several ways to grep lines

April 24th, 2008

Filed under: Linux, Ruby — Kai @ 12:17 am

Especially when accessing logfiles or large configfiles you often look for a particular pattern in it.

Everybody working with Linux shell usually appreciates grep.
I will just give you a short overview before getting around with the really worth knowing things:

All lines that match contain “EE” in general.log or mylog.log

grep -i 'd' general.log mylog.log

To lists the names of all files in the current directory whose contents mention “EE”.

grep -l 'EE' *.log


grep -lv
lists the names of all files containing one or more lines that do not match. To list the names of all files that contain no matching lines, use the -L or --files-without-match option.

You also can use regular expressions if needed.

For exclusively displaying lines starting with the string “root” just type:

grep ^root /etc/passwd

A cool addon to grep is egrep which can be used like sed (which shouldn’t be an issue here) to find & manipulate at a single blow.

This should delete all comments in the apache config.

egrep '^[^#]' /etc/apache2/apache2.conf

At least this is hardly correct, to match the comments it needs a bit more because they are most times followed by some whitespaces.
Effectively remove those lines:

egrep -v '^ *(#|$)' /etc/apache2/apache2.conf

If you’re a fan of ruby you might like this.
In ruby you need only request that your string be tested against the regular expression:

ruby -pe 'next unless $_ =~ /regex/' < test.txt

For instance to get every line of test.txt that contains a time in the given format (HH:MM:SS):

ruby -pe 'next unless $_ =~ /(^|\s)[0-9]{2}:[0-9]{2}(:[0-9]{2})(\s)/' < test.txt

To print only lines of 30 characters or greater:

ruby -pe 'next unless $_.chomp.length >= 30' < test.txt

It’s just the beginning of easy matching with ruby, as you might expect,there are many more undreamed-of possibilities.

post Best Practice: Avoid using const on read-only variables

April 22nd, 2008

Filed under: .NET — Kai @ 9:59 am

I often mention the blindfold use of readonly and const keyword in C#. In general you can say that const variables get the value assigned at Compile time and are unchangeable once established. In opposite to that readonly variable get the value assigned at run time and are unchangeable once established.

A quick synopsis on the differences between ‘const’ and ‘readonly’:

const

  • Can’t be static.
  • Value is evaluated at compile time.
  • Initiailized at declaration only.

readonly

  • Can be either instance-level or static.
  • Value is evaluated at run time.
  • Can be initialized in declaration or by code in the constructor.

For a const thist would be typical:

public class MyClass
{
  public const double PI = 3.14159;
}

PI cannot be changed in the application anywhere else in the code as this will cause a compiler error.

Constants must be of an integral type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string), an enumeration, or a reference to null.

Constants can be marked as public, private, protected, internal, or protected internal.

As mentioned above a readonly can be assigned at run time. Due to that this is allowed:

public class MyClass
{
  public readonly double PI;
 
  public MyClass()
  {
    PI = 3.14159;
  }
}

A C# guideline defines that the best practise is to avoid const on read-only variables, therefor use the readonly keyword.

The reason why is very simple. Lets say you have the following code in a class library:

public class ServiceClass
{
    public const int MyConst = 12;
    public readonly int MyReadOnlyVar = 10;
}

And you have the following code in another assembly calling the code above.

ServiceClass service = new ServiceClass();
 
int someValue = ServiceClass.MyConst;
 
someValue = service.MyReadOnlyVar;

You deploy both the class library and the assembly calling it. After deployment, a change is being made to the class library. you change the values of MyConst to 15 and MyReadOnlyVar to 15. You now release the class library and life is good. Is it?

What’s the values of someValue in the two statements below after the second release of the class library?

int someValue = ServiceClass.MyConst;
 
someValue = service.MyReadOnlyVar;

Initially, when the compiler sees the line

int someValue = ServiceClass.MyConst;

it replaces ServiceClass.MyConst with 12.

At runtime the class library is not called for the value since it is a constant. As a result of this, the code calling into the class library is not aware that the value of ServiceClass.MyConst has changed. It will continue to use the old value 12 while the new value of MyConst is 15.

post Lightweight, and simple pattern matching function

April 18th, 2008

Filed under: C++ — Kai @ 8:08 pm

If it’s wanted to match a string against a pattern based on wild characters (* and ?). The function presented here is a simple one that does this matching.

Patterns can be:

  • bka-b*
  • bk*bonn
  • bka-bo?n
  • b*b?nn

This is a fast, lightweight, and simple pattern matching function.

The function has to be call the function with two arguments. The first one is the string to be compared with the pattern specified in the second arguments.

The return value is accoring to the state if I should match Exact, Any or AnyRepeat.

bool MatchPattern(const char* szCompare, const char* szPattern) 
{
    enum State 
    {
        Exact,        // exact match
        Any,        // ?
        AnyRepeat    // *
    };
 
    const char* szTmp = szCompare;
    const char* szPat = szPattern;
    const char* q = 0;
    int iState = 0;
 
    bool bMatch = true;
    while (bMatch && *szPat) 
    {
        if (*szPat == '*') 
        {
            iState = AnyRepeat;
            q = szPat+1;
        } 
        else if (*szPat == '?') 
            iState = Any;
        else 
            iState = Exact;
 
        if (*szTmp == 0) 
          break;
 
        switch (iState) 
        {
            case Exact:
                bMatch = *szTmp == *szPat;
                szTmp++;
                szPat++;
                  break;
            case Any:
                bMatch = true;
                szTmp++;
                szPat++;
                  break;
            case AnyRepeat:
                bMatch = true;
                szTmp++;
                if (*szTmp == *szPat) 
                  szPat++;
 
                  break;
        }
    }
 
    if (iState == AnyRepeat) 
        return (*szTmp == *szPat);
    else if (iState == Any) 
        return (*szTmp == *pszPat);
    else 
        return bMatch && (*szTmp == *pszPat);
}

Obviously usage is as expected:

if(MatchPattern("bka-bonn", "b*b?nn"))
{ 
//matches
}
else
{
//no match
}

Finally I have to tell you that function doesn’t perform any error checking.

For matching patterns you also can use regular expressions - a good regex c++ library is provided by boost.

Simple example for it’s usage:

boost::regex expr("abcd[a-z].*");
string line("bka-bonn");
 
     if (boost::regex_search(line, expr)) 
     {
        //matches
     } 
     else 
     {
        //no match
     }

Further information can be read in boost’s docs: Boost.Regex

Indeed regular expressions are much more flexible and powerful but if you just want to match with ? and * wild characters the above mentioned function might be a better solution.

As always you also here have to weigh one thing against another. You might wonder what is important performance or flexibility.
If you just need it at one certain point in your project and it will never be enhanced I’d choose the MatchPattern function cause it’s simple and you don’t have to use an external library.
Otherwise, if a lot of lines in your code are based on matching strings, boost’s regex library should be the recommented way.

post Book Recommendation: XAML in a Nutshell

April 16th, 2008

Filed under: .NET, Books, WPF — Kai @ 7:34 pm

XAML in a NutshellToday I’d like to recomment a book to you. It’s called XAML in a Nutshell (O’Reilly, 2006; ISBN: 0596526733) by Lori A. MacVittie.

For those of you who don’t know already XAML is a .NET technology. It’s a markup language that can be used to help create desktop applications, web pages, and printable documents.

It stands for Extensible Application Markup Language and is pronounced as zammel ([zæ:mɛl]).

XAML generally follows XML syntax rules, just as any other XML-based markup language does. Each XAML element has a name and one or more attributes. Attributes correspond directly to object properties, and the name of the XAML element exactly matches the name of a CLR class definition.

XAML is pure markup, which means that while the names of event handlers are specified as attributes, you must implement the actual logic of the event handler in code. If you’re familiar with ASP.NET programming techniques, then you’ll be familiar with the term codebehind, which refers to the code “behind” a XAML interface element that is responsible for providing application logic such as event handlers.
It can be implemented in either C# or VB.NET. In both cases, the code can be placed inline in the XAML file, although this contradicts best practices in separating the presentation and application logic layers.

A XAML file can be compiled into a .baml (Binary XAML) file, which may be inserted as a resource into a .NET Framework assembly. At run-time, the framework engine extracts the .baml file from assembly resources, parses it, and creates a corresponding WPF visual tree or workflow.

When used in Windows Presentation Foundation, XAML is used to describe visual user interfaces. WPF allows for the definition of both 2D and 3D objects, rotations, animations, and a variety of other effects and features.

Here’s an overview of what to expect:

  • Introducing XAML
  • Getting Started
  • XAML Basics
  • Layout and Positioning
  • Resources
  • Storyboards and Animations
  • Elements
  • Controls
  • Shapes and Geometry
  • Layout
  • Animations and Transformations
  • Events

Along with this, you get eight appendices covering the major parts of the XAML technology.

This excellent book gives the reader a quick reference to XAML with examples. In other words, this book provides documentation of all core components and presents detailed discussions on features such as animation, resources, and layout that will help you on your way to becoming a XAML developer.
Even if you’ve never before heard of XAML, this book will give a good starting point to begin your investigations into this new technology…

The best comes last. You can get a preview online on google’s books: [link]

post Basic but reusable: Reading CSV

April 16th, 2008

Filed under: .NET — Kai @ 9:54 am

First, let’s recite the rules of CSV: Each line in a text file represents a record. The fields on each line are separated by commas.

Generally a CSV file should be abide by this rules:

  • Each record is one line (with exceptions)
  • Fields are separated with commas
  • Leading and trailing space-characters adjacent to comma field separators are ignored
  • Fields with embedded commas must be delimited with double-quote characters
  • Fields that contain double quote characters must be surounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
  • A field that contains embedded line-breaks must be surounded by double-quotes
  • Fields with leading or trailing spaces must be delimited with double-quote characters
  • Fields may always be delimited with double quotes
  • The first record in a CSV file may be a header record containing column (field) names

Take for example the next trivial CSV file:

alice, bob, eve, trudy
alice said: "don't move","""I won't"", he replied"

The first line parses into four separate fields (”alice”, “bob”, “eve”, “trudy”). The second one is trickier, but it produces two values.

The easiest implementation would be this:

private void readFile(String filePath)
{
  TextReader tx;
            try {
                tx = new StreamReader(filePath);
                String line;
 
                while ((line = tx.ReadLine()) != null)
                {
                    parseData(line);
                }
 
                tx.Close();
            } 
            catch(IOException ex) 
            {
                Console.WriteLine(ex.Message);
            }
}

Can be simply used that way:

private void parseData(String d)
{
            String[] sepData = d.Split(',');
            foreach(String d in sepData)
            {
                Console.WriteLine(d);
            }
}

And as you can guess, the code produces output like this:

> alice
> bob
> eve
> trudy

Those are just basics but they are reusable in some projects.

post Creating an alias for a nested namespace

April 11th, 2008

Filed under: C++ — Kai @ 11:23 am

Namespace definitions hold declarations. Since a namespace definition is a declaration itself, namespace definitions can be nested.

An alias can also be applied to a nested namespace.

namespace INTERNATIONAL_BUSINESS_MACHINES {
  int j;
    namespace NESTED_IBM_PRODUCT {
      void a() { j++; }
      int j;
      void b() { j++; }
  }
}
namespace NIBM = INTERNATIONAL_BUSINESS_MACHINES::NESTED_IBM_PRODUCT

In this example, the NIBM identifier is an alias for the namespace NESTED_IBM_PRODUCT. This namespace is nested within the INTERNATIONAL_BUSINESS_MACHINES namespace.

ruldrurd
Next Page »
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)