使用perl / awk或其他替换带有匹配字符的ascii

时间:2015-06-05 19:24:18

标签: regex linux perl awk

我有一个用nginx和lua代码编写的访问日志。 它是url编码的,那些字符是以\ xHexCode的格式编写的(例如,双引号写为\ x22)。 我想运行awk或perl或其他快速脚本来替换它。

1 个答案:

答案 0 :(得分:2)

您可以像这样使用text:-

 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook,
        LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    const int WH_KEYBOARD_LL = 13;
    const int WM_KEYDOWN = 0x100;
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;

    private static IntPtr hhook = IntPtr.Zero;

    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                GetModuleHandle(curModule.ModuleName), 0);
        }
    }

     private static IntPtr HookCallback(
   int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            if (vkCode.ToString() == "162")
            {
                UnHook();
                Form2 f2 = new Form2();
                f2.ShowDialog();
                _hookID = SetHook(_proc);
            }
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }


    public static void UnHook()
    {
        UnhookWindowsHookEx(_hookID);
    }

    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        UnHook();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

        _hookID = SetHook(_proc);
    }

这是它的工作方式:

  1. 使用gnu-awk我们为每个str='\x22 \x41 written as \x22).' awk -v RS='\\\\x[0-9]+' 'RT{ORS=sprintf("%c", strtonum("0" substr(RT, 2)))} 1' <<< "$str" " A written as "). 号码分隔自定义记录分隔符。
  2. RS='\\\\x[0-9]+'\xNN
  3. 获取substr(RT, 2)
  4. x41添加\x41以使其strtonum("0" substr(RT, 2))并返回ascii代码0
  5. 0x4165打印等效的ascii字符printf "%c"
  6. A设置输出记录分隔符与65的返回值相同。