Tôi đã trải qua chuỗi này và tìm thấy soluti là tốt. Trong khi thử một giải pháp awk thấy rằng trong lịch sử nếu có nhiều dòng lệnh có hoặc không gian vv họ có thể có những thách thức để in những gì yêu cầu thực tế của OP là. Hãy để tôi đưa ra một ví dụ ở đây, tôi chạy một lệnh như thế này.
awk '
match ($0, /<property class="java.lang.String" name="WorkJobNumber" value="[0-9]*"\/>/) {sub (substr ($0, RSTART+63, RLENGTH-66), _)
}
1123
' Input_file
Vì vậy, trong lịch sử nó sẽ đến dưới 1 số thứ tự nhưng có thể là vấn đề nếu điều này bị bỏ qua trong giải pháp. Vì vậy, sau đây có thể tránh một tình huống mà các lệnh đa dòng đặc biệt đã được chạy.
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'
A-one phi hình thức lót của giải pháp quá như sau:
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
'
Giải thích về mã trên như sau quá (người ta không nên chạy sau khi nó chỉ dành cho mục đích giải thích):
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.
Mặc dù lệnh này cũng có thể có một số thách thức, đã cố gắng hết sức để giữ cho nó hoàn hảo, mở để phản hồi hoặc đề xuất.
Can chúng ống trong đầu ra từ 'lệnh history' thay vì đọc các tập tin? – cwd
Dường như làm việc! Bạn có thể giải thích những gì nó đang làm? Nó sẽ hoạt động nếu các con số là 1 - 10.000? – cwd
Nó khá thô - bạn có thể làm tốt hơn với sed hoặc awk. 'cut' ở đây chỉ là tước bỏ 7 ký tự đầu tiên của mỗi dòng. –