尽管使用 -c 选项进行剪切适用于大多数实际目的,但我认为将管道历史记录到 awk 将是一个更好的解决方案。例如:
history | awk '{ $1=""; print }'
或
history | awk '{ $1=""; print $0 }'
这两种解决方案都做同样的事情。历史的输出被馈送到 awk。然后,Awk 将第一列清空,该列对应于历史命令输出中的数字。这里 awk 更方便,因为您不必关心输出的数字部分中的字符数。
print $0 等价于print,因为默认是打印行上出现的所有内容。键入print $0 更明确,但您选择哪一个取决于您。如果您使用 awk 打印文件,print $0 和简单的 print 与 awk 一起使用时的行为会更加明显(cat 会比 awk 更快地键入,但这是为了说明一点)。
[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."