我的数据文件a.alm
如下:
2018-05-19 00:26:00,551 [LUNSC1 ] D_TA204_GT1_DV_AL CFN LARM D_TA204_GT1_Reglerfel 2018-05-19 00:28:01,049 [LUNSC1 ] D_TA204_GT41_DV_AL CFN LARM D_TA204_GT41_Reglerfel 2018-05-19 00:28:01,049 [LUNSC1 ] D_TA204_GT31_DV_AL CFN LARM D_TA204_GT31_Reglerfel 2018-05-19 00:28:01,049 [LUNSC1 ] D_TA204_GT21_DV_AL CFN LARM D_TA204_GT21_Reglerfel 2018-05-19 00:35:19,627 [LUNSC1 ] U_TA364_GT11_LARM CFN LARM U_TA364_GT11_LARM 2018-05-19 00:39:56,135 [LUNSC1 ] U_TA364_GT11_LARM CFN LARM U_TA364_GT11_LARM 2018-05-19 00:47:12,612 [LUNSC1 ] U_KB6_GT11_DV_AL CFN LARM U-KB6-GT11 Avvikelselarm
我想将令牌5和8提取到另一个文件中。在我的代码中,我读取文件的内容并使用split函数循环。
$alarmList = @(Get-Content -Path '.\A.ALM') | foreach {
$test = $_ -split ' '
Write-Host $test[6]" "$test[9]
} | Out-File -FilePath '.\Output.txt'
输出仅给我第一个令牌。而且我无法理解如何正确地将其打印到文件中。尝试将Write-Host
替换为File-Output
,但收效不佳。
D_TA204_GT1_DV_AL D_TA204_GT41_DV_AL D_TA204_GT31_DV_AL D_TA204_GT21_DV_AL U_TA364_GT11_LARM U_TA364_GT11_LARM U_KB6_GT11_DV_AL U_FF415_GT46_L_AL U_KB6_GT11_DV_AL U_KB6_GT11_DV_AL U_KB6_GT11_DV_AL
我之前在命令行中使用此脚本对此进行了管理。
FOR /F "tokens=5" %%i in (A.alm) do echo %%i >> data.txt
但是我想在PowerShell中进行处理并将其通过管道传输,以能够使用我已经拥有的计数发生次数的脚本中的数据。
(Select-String -Path '.\data.txt' -Pattern '[^ ]+' -AllMatches).Matches.Value |
Group-Object -NoElement |
ConvertTo-Csv -Delimiter "`t" -NoTypeInformation |
Out-File -FilePath '.\Statistics.csv'
这很好,我了解得最多。
$alarmList = @(Get-Content -Path '.\A.ALM') | foreach {
$token = $_ -split ' +'
[PSCustomObject]@{
date = $token[0]
time = $token[1]
H2 = $token[2]
H3 = $token[3]
H4 = $token[4]
H5 = $token[5]
H6 = $token[6]
H7 = $token[7]
H8 = $token[8]
}
}
$alarmList | Group H4 -NoElement | Select Name,Count | Out-Gridview
答案 0 :(得分:3)
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
HWND tabControl;
void addTab(char* title, HWND tab);
HWND getSelectedTab();
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX mainwcex;
mainwcex.cbSize = sizeof(WNDCLASSEX);
mainwcex.style = CS_HREDRAW | CS_VREDRAW;
mainwcex.lpfnWndProc = WindowProc;
mainwcex.cbClsExtra = 0;
mainwcex.cbWndExtra = 0;
mainwcex.hInstance = hInstance;
mainwcex.hIcon = NULL;
mainwcex.hCursor = (HICON)LoadCursor(NULL, IDC_ARROW);
mainwcex.hbrBackground = GetSysColorBrush(COLOR_MENU);
mainwcex.lpszMenuName = NULL;
mainwcex.lpszClassName = "mainwindow";
mainwcex.hIconSm = NULL;
RegisterClassEx(&mainwcex);
HWND mainWindow = CreateWindowEx(
NULL,
"mainwindow",
NULL,
WS_OVERLAPPEDWINDOW,
100,
100,
560,
540,
NULL,
NULL,
hInstance,
NULL);
tabControl = CreateWindowEx(
NULL,
WC_TABCONTROL,
NULL,
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
10,
10,
525,
450,
mainWindow,
NULL,
hInstance,
NULL);
HWND okButton = CreateWindowEx(
NULL,
WC_BUTTON,
"OK",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
352,
466,
88,
26,
mainWindow,
NULL,
hInstance,
NULL);
HWND cancelButton = CreateWindowEx(
NULL,
WC_BUTTON,
"Cancel",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
446,
466,
88,
26,
mainWindow,
NULL,
hInstance,
NULL);
mainwcex.hbrBackground = CreateSolidBrush(RGB(214,234,248));
mainwcex.lpszClassName = "bluetab";
RegisterClassEx(&mainwcex);
HWND blueTab = CreateWindowEx(
NULL,
"bluetab",
NULL,
WS_CHILD | WS_VISIBLE,
10,
30,
505,
410,
tabControl,
NULL,
hInstance,
NULL);
HWND blueTabButton = CreateWindowEx(
NULL,
WC_BUTTON,
"Blue Btn",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
100,
100,
88,
26,
blueTab,
NULL,
hInstance,
NULL);
mainwcex.hbrBackground = CreateSolidBrush(RGB(213, 255, 200));
mainwcex.lpszClassName = "greentab";
RegisterClassEx(&mainwcex);
HWND greenTab = CreateWindowEx(
NULL,
"greentab",
NULL,
WS_CHILD,
10,
30,
505,
410,
tabControl,
NULL,
hInstance,
NULL);
HWND greenTabButton = CreateWindowEx(
NULL,
WC_BUTTON,
"Green Btn",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
100,
100,
88,
26,
greenTab,
NULL,
hInstance,
NULL);
addTab("Blue Tab", blueTab);
addTab("Green Tab", greenTab);
SetFocus(tabControl);
ShowWindow(mainWindow, nCmdShow);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (!IsDialogMessage(mainWindow, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return static_cast<int>(msg.wParam);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_NOTIFY:
{
NMHDR* nmhdr = (NMHDR*) lParam;
UINT notificationCode = nmhdr->code;
if (notificationCode == TCN_SELCHANGING) {
ShowWindow(getSelectedTab(), SW_HIDE);
} else if (notificationCode == TCN_SELCHANGE) {
ShowWindow(getSelectedTab(), SW_SHOWNORMAL);
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void addTab(char* title, HWND tab) {
TCITEM tcItem;
ZeroMemory(&tcItem, sizeof(RECT));
tcItem.mask = TCIF_TEXT | TCIF_PARAM;
tcItem.pszText = title;
tcItem.cchTextMax = 10;
tcItem.lParam = (LPARAM) tab;
int lastIndex = (int) SendMessage(tabControl, TCM_GETITEMCOUNT, NULL, NULL);
SendMessage(tabControl, TCM_INSERTITEM, lastIndex, (LPARAM)&tcItem);
}
HWND getSelectedTab()
{
TCITEM tcItem;
ZeroMemory(&tcItem, sizeof(RECT));
tcItem.mask = TCIF_PARAM;
LRESULT selectedIndex = SendMessage(tabControl, TCM_GETCURSEL, NULL, NULL);
SendMessage(tabControl, TCM_GETITEM, selectedIndex, (LPARAM) &tcItem);
return (HWND) tcItem.lParam;
}
将连续的定界符(默认为空格)计为一个
for /f
只是为了演示-split ' +'
运算符基于正则表达式的结果:
-split
简短的a.alm输出:
(Get-Content -Path '.\A.ALM') | foreach {
$token = $_ -split ' +'
for ($i=0;$i -le 8;$i++){
"token[{0}]={1}" -f $i,$token[$i]
}
}
要让$ alarmList包含所有这些列:
token[0]=2018-05-19
token[1]=00:47:12,612
token[2]=[LUNSC1
token[3]=]
token[4]=U_KB6_GT11_DV_AL
token[5]=CFN
token[6]=LARM
token[7]=U-KB6-GT11
token[8]=Avvikelselarm
$alarmList = @(Get-Content -Path '.\A.ALM') | foreach {
$token = $_ -split ' +'
[PSCustomObject]@{
date = $token[0]
time = $token[1]
H2 = $token[2]
H3 = $token[3]
H4 = $token[4]
H5 = $token[5]
H6 = $token[6]
H7 = $token[7]
H8 = $token[8]
}
}
$alarmList | ft -auto
当然,您可以进一步玩它:
date time H2 H3 H4 H5 H6 H7 H8
---- ---- -- -- -- -- -- -- --
2018-05-19 00:26:00,551 [LUNSC1 ] D_TA204_GT1_DV_AL CFN LARM D_TA204_GT1_Reglerfel
2018-05-19 00:28:01,049 [LUNSC1 ] D_TA204_GT41_DV_AL CFN LARM D_TA204_GT41_Reglerfel
2018-05-19 00:28:01,049 [LUNSC1 ] D_TA204_GT31_DV_AL CFN LARM D_TA204_GT31_Reglerfel
2018-05-19 00:28:01,049 [LUNSC1 ] D_TA204_GT21_DV_AL CFN LARM D_TA204_GT21_Reglerfel
2018-05-19 00:35:19,627 [LUNSC1 ] U_TA364_GT11_LARM CFN LARM U_TA364_GT11_LARM
2018-05-19 00:39:56,135 [LUNSC1 ] U_TA364_GT11_LARM CFN LARM U_TA364_GT11_LARM
2018-05-19 00:47:12,612 [LUNSC1 ] U_KB6_GT11_DV_AL CFN LARM U-KB6-GT11 Avvikelselarm
# $alarmList | Out-Gridview
# $alarmList | Export-csv '.\Output.csv' -NoTypeInformation
$alarmList | Group H4 -NoElement | Select Name,Count
答案 1 :(得分:1)
您可以使用Select-Object
和-Index
参数选择要提取的行的索引,最后将其存储到Output.txt
文件中。
$file = Get-Content .\A.ALM
$file | Select-Object -Index 6,9 | Out-File -FilePath '.\Output.txt'