如何将文件的一部分移动到末尾

发布于 2024-08-02 05:51:31 字数 980 浏览 7 评论 0原文

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 中,它意味着粘贴)。

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

rpm automatically place a new installed kernel as the first option. However, I want to move it as the last one - to end of the file.

Grub configuration file looks like this:

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

My goal is to move first option (217.2.3) to end. Now I figure out how to delete it:

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

p command only prints current line (not as in vim, where it means paste).

Do you have any ideas how to automatically move this part of file to its end?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-08-09 05:51:31

我必须自己回答。 :-)

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

I have to answer myself. :-)

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

If you are not fluent with sed (me neither), there is more verbose version

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
自控 2024-08-09 05:51:31

广泛涵盖了一个非常相似的任务这里

是的,精心制作的 sed 命令让人感到满意,但我想我会倾向于使用编辑器,这样我就可以看到我要移动的行,而不必担心关于命令中的行号错误。

A very similar task was covered extensively here

Yes, there is some satisfaction in a well crafted sed command, but I think I would tend to use an editor, so I could see the lines I was going to move around, and not have to worry about getting the line numbers wrong in a command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文