我正在通過此線程並找到soluti ons是好的。在嘗試使用awk解決方案時,發現歷史上如果存在多行命令或空格等,他們可能會面臨打印OP實際要求的挑戰。讓我舉一個例子,我這樣做了一個命令。
awk '
match ($0, /<property class="java.lang.String" name="WorkJobNumber" value="[0-9]*"\/>/) {sub (substr ($0, RSTART+63, RLENGTH-66), _)
}
1123
' Input_file
所以在歷史上它將會在1序列號來,但如果這是在解決方案/ s的忽視可能是一個問題。所以下面的例子可以避免特別是多行命令被運行的情況。
history | awk '{Q=$1;sub(/^[[:space:]]+/,"",Q);if(LAST+1==Q && LAST && Q ~ /^[0-9]+/){;$1="";print;} else {print;};if(Q ~ /^[0-9]+/ && Q+0 == LAST+1 && $0 !~ /^$/){LAST=Q+0};next} (Q ~ /^[0-9]+/ && $0 !~ /^$/){LAST=Q+0} 1'
的溶液的非一個襯裏形式太如下:
history | awk '{
Q=$1;
sub(/^[[:space:]]+/,"",Q);
if(LAST+1==Q && LAST && Q ~ /^[0-9]+/){;
$1="";
print;
}
else {
print;
};
if(Q ~ /^[0-9]+/ && Q+0 == LAST+1 && $0 !~ /^$/){
LAST=Q+0
};
next
}
(Q ~ /^[0-9]+/ && $0 !~ /^$/){
LAST=Q+0
}
1
'
的上面的代碼說明作爲太如下(一個不應該運行以下,因爲它僅用於解釋的目的):
history | #### Running history command here and using pipe to use this command's standard output to standard input for next awk command.
awk ' #### Starting awk command from here.
{Q=$1 #### Starting a variable named Q, whose value is $1 of current line.
sub(/^[[:space:]]+/,"",Q) #### subsitute initial space of variable Q(which has $1 of current line too) to NULL now.
if(LAST+1==Q && LAST && Q ~ /^[0-9]+/) #### mentioning here a if condition which is checking following conditions.
i- check if variable named LAST's value +1 is equal to variable Q's value(where LAST variable is the variable which has previous line's number.
ii- check LAST's value should NOT be NULL.
iii- check variable Q's value should be always starting from digits(As running multi-lie commands eg--> awk you could have commands there so making sure our LAST variable doesn't have any junk in it).
{;$1="";print;} #### making $1's value NULL so that history number will not print and then printing the current line.
else #### Mentioning else, in case above if condition is NOT TRUE then following statements should execute.
{;print} #### simply print the current line.
;if(Q ~ /^[0-9]+/ && Q+0 == LAST+1 && $0 !~ /^$/) #### checking following conditions here.
i- check if variable Q's value always starts from digits to make sure no junk will come apart from history numbers in variable here.
ii- check Q+0 == LAST+1, here Q+0 I am mentioning because in my history it was showing 772*(in a row) so Q+0 will make sure only numbers are being captured here. then comparing it with LAST+1 value, if both are equal here.
iii- Making sure each line starts NOT from a space(in multi-line commands you may see spaces).
{LAST=Q+0};next} #### Assigning variable LAST's value to Q's value(by doing Q+0 making sure like only digits should be captured here). Mentioning next here which is awk's in-built keyword and will skip all next statements then.
(Q ~ /^[0-9]+/ && $0 !~ /^$/) #### This condition will be executed when previous are not true, checking Q's value should be starting from digits only to get the history's sequence number only and checking a line shouldn't start from space etc.
{LAST=Q+0} #### Assigning variable LAST's value to value of Q+0 which will make sure only digits should come.
1' #### So awk works on pattern{action} method, so by mentioning 1 I am making a pattern/condition true and then not mentioning any action here so by default print action will happen and it will print the current line, it will happen only in those cases when a history doesn't have any sequence number in it.
雖然這個命令也可能有一些挑戰,盡我所能,保持完美無缺,反饋或建議。
我們能否管從'history'命令的輸出,而不是讀的文件? – cwd
似乎工作!你能解釋它在做什麼嗎?如果數字是1 - 10,000,它會起作用嗎? – cwd
這非常粗糙 - 你可以用sed或awk做得更好。這裏的「剪切」只是刪除每行的前7個字符。 –