201309220045


HTMLайзер таблиц: htmlize.sh

  #!/bin/sh

  echo "\0357\0273\0277"
  echo "<html><head><style>"
  echo "table,th,td{border-collapse:collapse;border:1px solid #000000;}"
  echo "</style></head><body><table border>"
  cat $1 | head -n 2 | sed "s/^  +-.*//g" | grep -v "^$" |               \
      sed "s/^  |/<tr><th>/g" | sed "s/|$/<\/th><\/tr>/g" |              \
      sed "s/|/<\/th><th>/g" | sed "s/ /\&nbsp;/g"
  more +3 $1 | sed "s/^  +-.*//g" | grep -v "^$" |                       \
      sed "s/^  |/<tr><td>/g" | sed "s/|$/<\/td><\/tr>/g" |              \
      sed "s/|/<\/td><td>/g" | sed "s/ /\&nbsp;/g"
  echo "</table></body><html>"

Пример использования
  
  $ cat numbers.txt
  
    +---+----+----+----+----+----+----+----+----+----+----+
    |   | SP | I  | II | III| IV | V  | VI | VII|VIII| IX |
    +---+----+----+----+----+----+----+----+----+----+----+
    | 0 | 0  | 1  | 2  | 3  | 4  | 5  | 6  | 7  | 8  | 9  |
    +---+----+----+----+----+----+----+----+----+----+----+
    | 1 | A  | B  | C  | D  | E  | F  | G  | H  | I  | J  |
    +---+----+----+----+----+----+----+----+----+----+----+
    | 2 | K  | L  | M  | N  | O  | P  | Q  | R  | S  | T  |
    +---+----+----+----+----+----+----+----+----+----+----+
    | 3 | U  | V  | W  | X  | Y  | Z  | a  | b  | c  | d  |
    +---+----+----+----+----+----+----+----+----+----+----+
    | 4 | e  | f  | g  | h  | i  | j  | k  | l  | m  | n  |
    +---+----+----+----+----+----+----+----+----+----+----+
    | 5 | o  | p  | q  | r  | s  | t  | u  | v  | w  | x  |
    +---+----+----+----+----+----+----+----+----+----+----+
    | 6 | y  | z  |    |    |    |    |    |    |    |    |
    +---+----+----+----+----+----+----+----+----+----+----+

  $ sh htmlize.sh numbers.txt > n.html
  $ firefox n.html
  
    HTML таблица

========================================================================

Мини cat

  $ csc4 cat.cs

  Tue 09/24/201310:59:58.75 $ mode con: cp select=437

  Status for device CON:
  ----------------------
      Lines:          43
      Columns:        132
      Keyboard rate:  31
      Keyboard delay: 1
      Code page:      437

  Tue 09/24/201310:59:58.78 $ C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc cat.cs
  Microsoft (R) Visual C# Compiler version 4.0.30319.32559
  for Microsoft (R) .NET Framework 4.5
  Copyright (C) Microsoft Corporation. All rights reserved.

  Tue 09/24/201310:59:59.08 $ mode con: cp select=65001

  Status for device CON:
  ----------------------
      Lines:          43
      Columns:        132
      Keyboard rate:  31
      Keyboard delay: 1
      Code page:      65001
      
  $ type cat.cs
  
  using System;
  using System.IO;
  using System.Linq;

  public class cat {
    static void Main(string[] args) {
      var nonSwitches = from arg in args 
                        where arg.FirstOrDefault() != '/' 
                        select arg;
      var fname = nonSwitches.FirstOrDefault();

      Console.WriteLine("Читаю тело файла:");
      StreamReader s = File.OpenText( fname);
      string read = null;
      while (( read = s.ReadLine()) != null) {
        Console.WriteLine(read);
      }
      s.Close();
    }
  }
  
Пример использования

  Tue 09/24/201311:03:35.36 $ ucat numbers.txt
  Читаю тело файла:

    +---+----+----+----+----+----+----+----+----+----+----+
    |   | SP | I  | II | III| IV | V  | VI | VII|VIII| IX |

    . . .
    
========================================================================

Фильтр-заготовка

Пример использования:

  Tue 09/24/201310:40:51.12 $ type numbers.txt | ss

    +---+----+----+----+----+----+----+----+----+----+----+
    |   | SP | I  | II | III| IV | V  | VI | VII|VIII| IX |

    . . .
    
  $ type ss.cs

  using System;

  public class HelloWorld {
    static void Main() {
      string line;
      while ((line = Console.ReadLine()) != null) {
        Console.WriteLine("{0}", line);
      }
    }
  }
  
========================================================================

Буферизуем. Эхо строчек ввода.

  using System;
  using System.Collections.Generic;

  public class HelloWorld {
    static void Main() {
      List<string> input = new List<string>();
      string line;
      while ((line = Console.ReadLine()) != null && line != "") {
        input.Add(line);
      }
      foreach (string item in input)
        Console.Write(item.ToString() + "\n");
    }
  }

========================================================================

Список файлов в текущем каталоге.

  // [1]. http://stackoverflow.com/questions/1360183                   \
  //      /how-do-i-call-a-non-static-method-from-a-static-method-in-c

  using System;
  using System.IO;

  class C {
    public void ls() {
      string curr = Directory.GetCurrentDirectory();
      string[] filePaths = Directory.GetFiles( curr);

      Console.Write( 
        "\nПапка: " + 
        curr + 
        "\n\n  Длина     Имя Файла\n\n"
      );
      
      foreach (string filePath in filePaths) {
        FileInfo file = new FileInfo( filePath);
        long f = file.Length;
        Console.Write( 
          "  " + 
          f.ToString().PadRight(10) + 
          Path.GetFileName( filePath) + 
          '\n'
        );
      }
    }
    
    static void Main(string[] args) {
      C c = new C(); // [1]
      c.ls();
    }
  }

========================================================================

Grep. (вытряхнул некоторое количество строк из оригинальной версии).

Компилируем в csc4 либо csc4-64:

  $ csc4-64 ugrep.cs
  
Пример использования:

  Tue 09/24/2013 9:37:21.13 $ type unix.txt | ugrep /i " [а-я]{18} "

Исходный файл:

  using System;
  using System.IO;
  using System.Text.RegularExpressions;

  class u8grep {
    static void Main(string[] args) {
      // Parse command-line switches
      bool ignoreCase = args.Contains("/i");

      var nonSwitches = from arg in args 
                        where arg.FirstOrDefault() != '/' 
                        select arg;
      var regexString = nonSwitches.FirstOrDefault();

      string line;
      while ((line = Console.ReadLine()) != null) {
        Match m = Regex.Match(line, regexString);
        if (m.Success)
          Console.WriteLine("{0}", line);
      }
    }
  }
  
========================================================================

ParallelGrep. 

Взято на:

  http://code.msdn.microsoft.com/windowsdesktop/Samples-for-Parallel-b4b76364
  
Компилируем в csc4 либо csc4-64:

  $ csc4-64 ugrep.cs
  
Пример использования:

  Tue 09/24/2013 9:37:21.13 $ ugrep /i " [а-я]{3} " *.cs

Исходный файл:

  //--------------------------------------------------------------------------
  // 
  //  Copyright (c) Microsoft Corporation.  All rights reserved. 
  //
  //--------------------------------------------------------------------------

  using System;
  using System.IO;
  using System.Linq;
  using System.Text.RegularExpressions;
  using System.Threading;

  class u8grep {
    static void Main(string[] args) {
    
      // Parse command-line switches
      
      bool recursive = args.Contains("/s");
      bool ignoreCase = args.Contains("/i");

      // Get the regex and file wildcards from the command-line
      
      var nonSwitches = from arg in args 
                        where arg.FirstOrDefault() != '/' 
                        select arg;
      var regexString = nonSwitches.FirstOrDefault();
      var wildcards = nonSwitches.Skip(1);

      // Create thread-local Regex instances, 
      // to prevent contention on Regex's internal lock
      
      var regex = new ThreadLocal<Regex>(() =>
        new Regex(regexString, RegexOptions.Compiled | 
        (ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)));

      // Get the list of files to be examined
      
      var files = from wc in wildcards
        let dirName = Path.GetDirectoryName(wc)
        let fileName = Path.GetFileName(wc)
        from file in Directory.EnumerateFiles(
            String.IsNullOrWhiteSpace(dirName) ? "." : dirName,
            String.IsNullOrWhiteSpace(fileName) ? "*.*" : fileName,
            recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
        select file;

      try {
      
        // Traverse the specified files in parallel, and run each 
        // line through the Regex, collecting line number info
        // for each match (the Zip call counts the lines in each file)
        
        var matches = from file in files.AsParallel().AsOrdered().WithMergeOptions(ParallelMergeOptions.NotBuffered)
                      from line in File.ReadLines(file).Zip(Enumerable.Range(1, int.MaxValue), (s, i) => 
                        new { Num = i, Text = s, File = file })
                      where regex.Value.IsMatch(line.Text)
                      select line;
        foreach (var line in matches) {
          Console.WriteLine("{0}:{1} {2}", line.File, line.Num, line.Text);
        }
      }
      catch (AggregateException ae) {
        ae.Handle(e => { Console.WriteLine(e.Message); return true; });
      }
    }
  }

