Sed 如何从历史记录中复制命令?

Sed 如何从历史记录中复制命令?,sed,awk,history,Sed,Awk,History,我试图从历史记录中复制命令。如何复制第510个命令?请看下面的数据。我打赌: history | grep 510 | sed '1q;d' | awk '{print $2-$10}' | pbcopy 但输出为0。我不明白原因。命令有什么问题 505 find . -perm=750 -print0 | xargs -0 chmod 750 506 find . --perm=750 -print0 | xargs -0 chmod 750 507 find . -per

我试图从历史记录中复制命令。如何复制第510个命令?请看下面的数据。我打赌:

history | grep 510 | sed '1q;d' | awk '{print $2-$10}' | pbcopy
但输出为0。我不明白原因。命令有什么问题

  505  find . -perm=750 -print0 | xargs -0 chmod 750
  506  find . --perm=750 -print0 | xargs -0 chmod 750
  507  find . -perm=750 -print0 | xargs -0 chmod 750
  508  find . -perm=750 -print0 | xargs -0 chmod 750
  510  find . -perm 750 -print0 | xargs -0 chmod 750
  512  history | grep perm 750 -print0 | pbcopy

您可以使用perl打印除请求行的第一列(
510
)以外的所有内容:

history | perl -ane 'print "@F[1..$#F]" if($F[0]==510)'

原因是
$2
$10
不是awk脚本中的数字。字段的索引是基于1的,因此如果您想要
750
,第一个是
$5
,第二个是
$11

请注意,如果您使用的是bash,则这与带有“
-perm=750
”的行不同:

echo "!510" | pbcopy

(另请参见中的“历史扩展”)

有关Awk中的一系列字段,您需要一个For循环。你所做的是减法,因此结果是零

通常,cut命令执行提取列的任务,但有时Awk更合适

这就是你的意思

history | grep 510 | sed 1q \
 | awk '{for(i = 2; i <= NF; i++){ORS = (i == NF ? "\n" : " "); print $i;}}' \
 | pbcopy
history | grep 510 | sed 1q\

|awk'{for(i=2;我想这个人想要的是一系列的字段,而不是减法。不过这是惊人的巧合!@UnixBasics:不要试图在没有引号的情况下使用它,它会伤害你的。