Bash历史没有行号
bash history
命令非常酷。 我明白为什么它显示行号,但有没有办法我可以调用历史命令,并抑制行号?
这里的要点是使用历史命令,所以请不要回复cat ~/.bash_history
电stream输出:
529 man history 530 ls 531 ll 532 clear 533 cd ~ 534 histor y
历史graphics来源。
期望的输出:
man history ls ll clear cd ~ histor y
历史graphics来源。
感谢大家为您提供的绝佳解决scheme。 保罗的是最简单的,将为我工作,因为我的bash历史大小定在2000年。
我也想分享今天早上发现的一篇很酷的文章。 它有几个很好的select,我现在正在使用,如保持重复条目的bash历史logging,并确保多个bash会话不覆盖历史文件: http : //blog.macromates.com/2008/working-with -history合的bash /
尝试这个:
$ history | cut -c 8-
awk
可以帮助:
history|awk '{$1="";print substr($0,2)}'
如果你有一个长期的问题,这个答案可能会失败。
HTH
或者,您可以使用sed:
history | sed 's/^[ ]*[0-9]\+[ ]*//'
使用别名,你可以把它设置为你的标准(把它粘在你的bash_profile中):
alias history="history | sed 's/^[ ]*[0-9]\+[ ]*//'"
我很清楚,这个问题是为bash和许多人宁愿不切换到zsh(提示downvotes …)
然而,如果你愿意切换到zsh,那么zsh支持这个本地(以及历史格式的其他选项)
zsh> fc -ln 0
(请参阅https://serverfault.com/questions/114988/removing-history-or-line-numbers-from-zsh-history-file )
history
命令没有压缩行号的选项。 你将不得不按照每个人的build议合并多个命令:
例如:
history | cut -d' ' -f4- | sed 's/^ \(.*$\)/\1/g'
我迟到了,但更短的方法是在~/.bashrc
或~/.profile
文件中添加以下内容:
HISTTIMEFORMAT="$(echo -e '\r\e[K')"
从bash manpage
:
HISTTIMEFORMAT 如果这个variables被设置,而不是null,它的值被用作a strftime的格式化string(3)打印关联的时间戳 每个历史logging由历史logging显示。 如果 这个variables被设置,时间戳被写入历史logging 文件,以便它们可以在整个shell会话中保留。 这用途 历史评论字符来区分时间戳 其他历史线。
使用这个function,一个聪明的黑客就是让variables“打印”一个回车符( \r
)并清除这行(ANSI代码K
)而不是一个实际的时间戳。
你可以使用command cut
来解决它:
从STDIN或文件中删除字段。
-
剪掉每一行STDIN的前十六个字符:
cut -c 1-16
-
删除给定文件的每一行的前十六个字符:
cut -c 1-16 file
-
剪掉从第三个字符到每行结尾的所有内容:
cut -c3-
-
切出每行的第五个字段,使用冒号作为字段分隔符(默认分隔符是tab):
cut -d':' -f5
-
切出每行的第2和第10个字段,用分号作为分隔符:
cut -d';' -f2,10
cut -d';' -f2,10
-
用空格作为分隔符,
cut -d' ' -f3-7
每行的3到7字段:cut -d' ' -f3-7
尽pipe使用-c
选项进行剪切可以达到最实用的目的,但我认为将历史logging传送到awk将是一个更好的解决scheme。 例如:
history | awk '{ $1=""; print }'
要么
history | awk '{ $1=""; print $0 }'
这两个解决scheme都做同样的事情。 历史的输出被送到awk。 Awk然后空出第一列,这对应于历史命令输出中的数字。 这里awk更方便,因为你不必关心输出数字部分的字符数。
print $0
相当于print
,因为默认打印出现在行上的所有内容。 inputprint $0
更明确,但您select哪一个取决于您。 如果使用awk打印文件( cat
会更快地input而不是awk,但是这是为了说明一个要点), print $0
的行为以及在awk中简单print
的行为更为明显。
[Ex]使用awk显示$ 0文件的内容
$ awk '{print $0}' /tmp/hello-world.txt Hello World!
[Ex]使用awk显示文件的内容而不显式$ 0
$ awk '{print}' /tmp/hello-world.txt Hello World!
[Ex]当历史行跨越多行时使用awk
$ history 11 clear 12 echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style." $ history | awk ' $1=""; {print}' clear echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style."
我正在通过这个线程,发现解决scheme是好的。 在尝试awk解决scheme时发现,在历史上,如果存在多行命令或空格等,他们可能会面临打印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个序列号下出现,但如果在解决scheme中被忽略,这可能是一个问题。 所以下面可以避免特别是多行命令运行的情况。
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'
非线性的解决scheme也如下:
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.
虽然这个命令也可能有一些挑战,尽我所能,保持完美无缺,反馈意见或build议。