从历史记录中删除特定行号

2024-04-25 01:10:27 发布

您现在位置:Python中文网/ 问答频道 /正文

命令grep -n blink ~/.bash_history输出包含blink的所有行。我需要一个只输出行号的命令,并在history -d linenum
上执行行号 在python中:

#list generated from command
linenumbers = [1,2,3,4,5]
for count in range(linenumbers):
   os.system("history -d {}".format(count))

我该怎么做?你知道吗


Tags: infrom命令bashforcounthistorygrep
1条回答
网友
1楼 · 发布于 2024-04-25 01:10:27

在bash中:

for offset in $(history | awk '/blink/ {print $1}' | tac)
do
    history -d $offset
done

您可以直接从history命令获得偏移量,无需使用grep生成行号。此外,您还需要反向删除这些行(因此使用tac),因为被删除的命令后面的命令的偏移量被下移。你知道吗

相关问题 更多 >