Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded awk for .NET?

Is there an AWK-implementation for .NET?

To be more concrete, here is an example of the kinds of problems I want to solve:

Article number        Price          Package day
10.1002-50            $50            Tuesday
10.1002-51            $40            Monday
10.1002-52            $50
10.1002-53            $50            Tuesday
#start prepackaged
20.2001-51            $1             Monday
20.2001-52            $30            Tuesday
10.1002-54            $10            Tuesday
10.1002-55
#end prepackaged
10.1001-50
10.1002-50            $50            Friday
10.1003-50
10.1004-50

I want to get the article numbers, but skip the prepackaged ones.

Here's how I'd imagine one could solve it with an AWK-implementation available in C#:

using Awk.Extensions;
string[][] output = System.IO.File.ReadLines("C:\Temp\input.txt")
    .Skip(1)
    .Awk("/start prepackaged/,/end prepackaged/ {next}; {print $1}")
    .Select(fields => fields[0])
    .Distinct()
    .ToArray();

foreach(var item in output)
{
    Console.WriteLine(item);
}

This would then ideally produce the output:

10.1002-50
10.1002-51
10.1002-52
10.1002-53
10.1001-50
10.1003-50
10.1004-50

Does something similar exist? Are there some other powerful, line-based scripting libraries for .NET I should consider?

Further investigation shows that there's Jawk, a Java implementation of awk, which can also compile AWK scripts to Java bytecode! There seems to be a direct port of this to C#, although it doesn't seem very mature.

Another project for Java, awk4j:

private static void case03() throws ScriptException {
  ScriptEngine engine = new ScriptEngineManager().getEngineByName("awk4j");
  CompiledScript obj = ((Compilable) engine).compile("BEGIN{ printf ARGV[1] }");  // コンパイル
  engine.put(ScriptEngine.ARGV, new String[] { "hello," });
  obj.eval();  // コンパイル済みのスクリプトを実行
  engine.put(ScriptEngine.ARGV, new String[] { "world!\n" });
  obj.eval();
}

Here's a similar question: Is there a .NET library which will give me the ability to run awk scripts in a .NET environment?

like image 635
johv Avatar asked Jan 19 '14 16:01

johv


1 Answers

There is AWK for .NET. AWK is great, and I use it on Linux.

like image 87
pawelek Avatar answered Oct 14 '22 21:10

pawelek