代码之家  ›  专栏  ›  技术社区  ›  izidor

如何将部分文件移动到其结尾

  •  2
  • izidor  · 技术社区  · 16 年前

    RPM自动将新安装的内核作为第一个选项。但是,我想将其作为文件的最后一个-到结尾移动。

    GRUB配置文件如下:

    default=0
    timeout=5
    splashimage=(hd0,0)/grub/splash.xpm.gz
    hiddenmenu
    title Fedora (2.6.29.6-217.2.7.fc11.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.29.6-217.2.7.fc11.x86_64 ro root=/dev/mapper/main-root rhgb quiet
        initrd /initrd-2.6.29.6-217.2.7.fc11.x86_64.img
    title Fedora (2.6.29.6-217.2.3.fc11.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.29.6-217.2.3.fc11.x86_64 ro root=/dev/mapper/main-root rhgb quiet
        initrd /initrd-2.6.29.6-217.2.3.fc11.x86_64.img
    title Fedora (2.6.29.6-213.fc11.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.29.6-213.fc11.x86_64 ro root=/dev/mapper/main-root rhgb quiet
        initrd /initrd-2.6.29.6-213.fc11.x86_64.img
    

    我的目标是结束第一个选项(217.2.3)。现在我想知道如何删除它:

    sed -e '/(2.6.29.6-217.2.7.fc11.x86_64)/,+3d' /boot/grub/menu.lst
    

    p命令只打印当前行(与vim中的不同,它的意思是粘贴)。

    您对如何自动将文件的这部分移动到其结尾有什么想法吗?

    2 回复  |  直到 16 年前
        1
  •  3
  •   izidor    16 年前

    我得自己回答。:-)

    sed '/\(2.6.18-157.el5\)/,+4 { H; d; }; $ { p; x; }' /boot/grub/menu.lst
    

    如果你对SED(我也是)不太流利,那就有更详细的版本了。

    sed '
     /\(2.6.18-157.el5\)/,+3 { #Find line which contains version of our kernel in parentheses and took also 3 following lines
      H # Append this line into buffer
      d # Delete line
     }
    
     $ { # On the last line
     p # Print current line
     x # Change current line with buffer and vice versa
     # Afterwards sed print current line => in our case deleted line
     }' /boot/grub/menu.lst
    
        2
  •  0
  •   Community Mohan Dere    8 年前

    一个非常相似的任务被广泛地涵盖 here

    是的,一个精心设计的命令有一些满足感,但是我想我会倾向于使用一个编辑器,这样我就可以看到我要移动的行,而不必担心命令中的行数出错。

    推荐文章