我正在通过此线程并找到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个字符。 –