我有一个用nginx和lua代码编写的访问日志。 它是url编码的,那些字符是以\ xHexCode的格式编写的(例如,双引号写为\ x22)。 我想运行awk或perl或其他快速脚本来替换它。
答案 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);
}
这是它的工作方式:
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 ").
号码分隔自定义记录分隔符。 RS='\\\\x[0-9]+'
从\xNN
substr(RT, 2)
x41
添加\x41
以使其strtonum("0" substr(RT, 2))
并返回ascii代码0
。0x41
从65
打印等效的ascii字符printf "%c"
。A
设置输出记录分隔符与65
的返回值相同。