我用
cat /var/log/apt/history.log > ~/Desktop/allhistory.log && zcat /var/log/apt/history.log*gz >> ~/Desktop/allhistory.log创建~/Desktop/allhistory.log.。
我能跑
sed -r -i.1 '/(^Commandline: apt-get --no-upgrade|^Commandline: apt full-upgrade|^Upgrade: |^End-Date: |^Error: |^Requested-By: |^Commandline: apt-get autoremove|^Commandline: apt autoremove|^Commandline: apt-get --yes|Commandline: apt-get purge|Commandline: apt purge|^Remove: |^Commandline: apt-get dist-upgrade|^Install: |Commandline: aptdaemon|--reinstall|^Commandline: apt-get --yes --no-install-recommends|^Purge:|^Reinstall )/d' allhistory.log排除我不想要的东西。但我也想包括
Commandline: packagekit role='update-packages'当我尝试
sed -r -i.1 '/(^Commandline: packagekit role=\'update-packages\'|^Commandline: apt-get --no-upgrade|^Commandline: apt full-upgrade|^Upgrade: |^End-Date: |^Error: |^Requested-By: |^Commandline: apt-get autoremove|^Commandline: apt autoremove|^Commandline: apt-get --yes|Commandline: apt-get purge|Commandline: apt purge|^Remove: |^Commandline: apt-get dist-upgrade|^Install: |Commandline: aptdaemon|--reinstall|^Commandline: apt-get --yes --no-install-recommends|^Purge:|^Reinstall )/d' allhistory.log我得到了
bash: syntax error near unexpected token `)'要在sed命令中成功地包含Commandline: packagekit角色=‘update-packages’,我需要做什么?
发布于 2019-07-09 14:39:59
问题归结为“如何在单引号内引用/使用单引号”?
这个答案 on stackoverflow.com解释了这一点。使用
sed -r -i.1 '/(^Commandline: packagekit role='"'"'update-packages'"'"'|^Commandline: apt-get --no-upgrade|
^Commandline: apt full-upgrade|
....|
^Reinstall )/d' allhistory.log)我增加了新行以提高可读性。)将它们放到实际的命令中。
基本上是:将每个内部'替换为'"'"'。这就是:
'结束第一个字符串,"'")中附加文字','开头如果你认为这是不可读的,那么你是对的。这是因为bash没有像其他语言那样有一个很好的字符串连接操作符。如果存在这样的运算符(例如,+),则如下所示:
sed … '...role=' + "'" + 'update-packages' + "'" + '|^Commandline…'
# ssssssss d sssssssssssssss d ssssssssssssss这里,ses表示一个单引号,ds表示单引号中间的单引号。由于bash中没有这样的+运算符,所以需要将它们粘合在一起,中间没有任何空格,这就给出了'"'"'。
https://askubuntu.com/questions/1157009
复制相似问题