Linux 没有行号的 Bash 历史记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7110119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash history without line numbers
提问by cwd
The bash history
command is very cool. I understand why it shows the line numbers, but is there a way I can invoke the history command and suppress the line numbers?
bashhistory
命令非常酷。我明白为什么它会显示行号,但是有没有办法调用 history 命令并抑制行号?
The point here is to use the history command, so please don't reply cat ~/.bash_history
这里的重点是使用history命令,所以请不要回复 cat ~/.bash_history
Current Output:
电流输出:
529 man history
530 ls
531 ll
532 clear
533 cd ~
534 history
Desired Output:
期望输出:
man history
ls
ll
clear
cd ~
history
Thanks to everyone for your great solutions. Paul's is the simplest and will work for me for because my bash history size is set at 2000.
感谢大家提供出色的解决方案。Paul's 是最简单的并且对我有用,因为我的 bash 历史记录大小设置为 2000。
I also wanted to share a cool article I found this morning. It has a couple good options that I am now using, like keeping duplicate entries out of the bash history and making sure multiple bash sessions don't overwrite the history file: http://blog.macromates.com/2008/working-with-history-in-bash/
我还想分享我今天早上发现的一篇很酷的文章。它有几个我现在正在使用的不错的选项,例如将重复条目保留在 bash 历史记录之外并确保多个 bash 会话不会覆盖历史文件:http: //blog.macromates.com/2008/working-with -bash 中的历史/
采纳答案by Paul R
Try this:
尝试这个:
$ history | cut -c 8-
回答by ring bearer
history
command does not have an option to suppress line numbers. You will have to combine multiple commands as everyone is suggesting:
history
命令没有抑制行号的选项。您将不得不按照每个人的建议组合多个命令:
Example :
例子 :
history | cut -d' ' -f4- | sed 's/^ \(.*$\)//g'
回答by Zsolt Botykai
awk
can help:
awk
可以帮助:
history|awk '{="";print substr(history | sed 's/^[ ]*[0-9]\+[ ]*//'
,2)}'
This answercan fail if you have a long history.
如果您有很长的历史,这个答案可能会失败。
回答by Manny D
Alternatively, you could use sed:
或者,您可以使用 sed:
alias history="history | sed 's/^[ ]*[0-9]\+[ ]*//'"
Using alias, you can set this as your standard (stick it in your bash_profile):
使用别名,您可以将其设置为标准(将其粘贴在您的 bash_profile 中):
$ hh -n
回答by Martin Dvorak
history | awk '{ =""; print }'
You may want to try https://github.com/dvorka/hstrwhich allows for "suggest box style" filtering of Bash history with (optional) metrics based ordering i.e. it is much more efficient and faster in both forward and backward directions:
您可能想尝试https://github.com/dvorka/hstr,它允许使用基于(可选)指标的排序对 Bash 历史进行“建议框样式”过滤,即它在向前和向后的方向上都更加高效和快速:
It can be easily bound to Ctrl-rand/or Ctrl-s
它可以很容易地绑定到Ctrl-r和/或Ctrl-s
回答by Suave-V
Although cut with the -c
option works for most practical purposes, I think that piping history to awk would be a better solution. For example:
尽管使用-c
选项cut对大多数实际目的都有效,但我认为将历史记录到 awk 将是更好的解决方案。例如:
history | awk '{ =""; print $ awk '{print $ awk '{print}' /tmp/hello-world.txt
Hello World!
}' /tmp/hello-world.txt
Hello World!
}'
OR
或者
$ 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 ' =""; {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."
Both of these solutions do the same thing. The output of history is being fed to awk. Awk then blanks out the first column, which corresponds to the numbers in the history command's output. Here awk is more convenient because you don't have to concern yourself with the number of characters in the number part of the output.
这两种解决方案都做同样的事情。历史的输出被馈送到 awk。然后,Awk 将第一列清空,该列对应于历史命令输出中的数字。这里 awk 更方便,因为您不必关心输出数字部分中的字符数。
print $0
is equivalent to print
, since the default is to print everything that appears on the line. Typing print $0
is more explicit, but which one you choose is up to you. The behavior of print $0
and simply print
when used with awk is more evident if you used awk to print a file (cat
would be faster to type instead of awk, but this is for illustrating a point).
print $0
等价于print
,因为默认是打印出现在行上的所有内容。打字print $0
更明确,但您选择哪一个取决于您。如果您使用 awk 来打印文件print $0
,print
当与 awk 一起使用时and simple的行为会更加明显(cat
键入会比 awk 更快,但这是为了说明一点)。
[Ex] Using awk to display the contents of a file with $0
[Ex] 使用 awk 显示 $0 文件的内容
zsh> fc -ln 0
[Ex] Using awk to display the contents of a file without explicit $0
[Ex] 使用 awk 显示文件内容,不显式 $0
HISTTIMEFORMAT If this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry displayed by the history builtin. If this variable is set, time stamps are written to the history file so they may be preserved across shell sessions. This uses the history comment character to distinguish timestamps from other history lines.
[Ex] Using awk when the history line spans multiple lines
[Ex] 当历史行跨越多行时使用 awk
history -w /dev/stdout
回答by Att Righ
I'm quite aware that this question is for bash and many people would prefer to not switch to zsh (cue downvotes...)
我很清楚这个问题是针对 bash 的,很多人宁愿不切换到 zsh(提示downvotes...)
However, if you were willing to switch to zshthen zsh supports this natively (as well as other options for history formatting)
但是,如果您愿意切换到zsh,那么 zsh 本身就支持此功能(以及其他历史格式选项)
##代码##(See https://serverfault.com/questions/114988/removing-history-or-line-numbers-from-zsh-history-file)
(参见https://serverfault.com/questions/114988/removing-history-or-line-numbers-from-zsh-history-file)
回答by iMil
I'm late on this one, but the shorter method would be to add the following in your ~/.bashrc
or ~/.profile
file:
我迟到了,但更短的方法是在您的~/.bashrc
或~/.profile
文件中添加以下内容:
HISTTIMEFORMAT="$(echo -e '\r\e[K')"
HISTTIMEFORMAT="$(echo -e '\r\e[K')"
From bash manpage
:
从 bash manpage
:
Using this capability, a smart hack consist in making the variable "print" a carriage return (\r
) and clear the line (ANSI code K
) instead of an actual timestamp.
使用此功能,巧妙的技巧在于使变量“打印”回车符 ( \r
) 并清除行(ANSI 代码K
)而不是实际时间戳。
回答by GEOKING
You can use command cut
to solve it:
您可以使用命令cut
来解决它:
Cut out fields from STDIN or files.
从 STDIN 或文件中剪切字段。
Cut out the first sixteen characters of each line of STDIN:
cut -c 1-16
Cut out the first sixteen characters of each line of the given files:
cut -c 1-16 file
Cut out everything from the 3rd character to the end of each line:
cut -c3-
Cut out the fifth field of each line, using a colon as a field delimiter (default delimiter is tab):
cut -d':' -f5
Cut out the 2nd and 10th fields of each line, using a semicolon as a delimiter:
cut -d';' -f2,10
Cut out the fields 3 through 7 of each line, using a space as a delimiter:
cut -d' ' -f3-7
切出STDIN每行的前16个字符:
cut -c 1-16
切出给定文件每行的前十六个字符:
cut -c 1-16 file
剪下从第 3 个字符到每行末尾的所有内容:
cut -c3-
切出每行的第五个字段,使用冒号作为字段分隔符(默认分隔符是制表符):
cut -d':' -f5
切出每行的第 2 个和第 10 个字段,使用分号作为分隔符:
cut -d';' -f2,10
切出每行的第 3 到第 7 个字段,使用空格作为分隔符:
cut -d' ' -f3-7
回答by czerny
From output of history --help
:
从输出history --help
:
-w write the current history to the history file
-w 将当前历史写入历史文件
It writes current history to specified file - /dev/stdout
in this case.
它将当前历史记录写入指定文件 -/dev/stdout
在这种情况下。