用awk或sed来的声明钉在特定行的末尾 [英] Using Awk or Sed to tack on a statement at the end of a specific line

查看:114
本文介绍了用awk或sed来的声明钉在特定行的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件我命名为 poscar1.cif ,我想在这个文件中的特定行插入变量的内容。

I have a file I named poscar1.cif, and I would like to insert the contents of a variable at a specific line in this file.

例如,行 24 ,目前写着:

_cell_length_a

我想钉的内容,我的变量 A (在我的函数定义为 A = 5.3827 )这样一来该行现在读取:

I would like to tack the contents of my variable a (defined in my function as a=5.3827) so that way the line now reads:

_cell_length_a 5.3827

有没有办法做到这一点使用awk或者sed?我使用的是bash脚本来完成这个(完整的脚本过大后,很遗憾)。

Is there a way to do this using sed or awk? I am using a bash script to accomplish this (the full script is too large to post, unfortunately).

推荐答案

由于老将 ED 实用程序不会得到足够的重视了:

Since the veteran ed utility doesn't get enough attention anymore:

a=5.3827

ed -s poscar1.cif <<EOF 
g/^_cell_length_a\$/ s//& $a/
w
EOF

ED 真正的编辑代替文件的 的,不像 SED -i 选项 [1]

ed truly edits a file in place, unlike sed with its -i option[1].

SED ED ,借了许多功能,因此在功能显著重叠,但也有重要的区别,其中一些表面在这里。

sed borrowed many features from ed, so there is significant overlap in functionality, but there are also important differences, some of which surface here.


  • -s 燮presses 主编的状态消息。

  • poscar1.cif 是输入文件的地方进行编辑。

  • &LT;&LT; EOF ... 这里包含 ED的命令-document - ED 要求其命令来自的标准输入的每个命令是在自己的行。

  • 克/ ^ _ cell_length_a \\ $ / ...是一个(基本)正则表达式(普通前pression)认为恰好是包含所有行匹配 _cell_length_a - 在先按g 确保如果没有匹配都没有错误报告。

    • 请注意, $ \\ 转义的壳的此时此地文档内(不严格必要的这个的实例,但好的做法)。

    • -s suppresses ed's status messages.
    • poscar1.cif is the input file to edit in place.
    • <<EOF ... is the here-document that contains the commands for ed - ed requires its commands to come from stdin and each command to be on its own line.
    • g/^_cell_length_a\$/ ... is a (basic) regex (regular expression) that matches all lines that exactly contain _cell_length_a - the g ensures that no error is reported if there's no match at all.
      • Note that the $ is \-escaped to protect it from interpretation by the shell inside the here-document (not strictly necessary in this instance, but good practice).

      • 请注意,由于开始分隔符( EOF )的这里文档是的不带引号的变量扩展DO发生;在本质上,其内容是由像一个双引号字符串的内容壳处理。

      • Note that since the opening delimiter (EOF) of the here-document is unquoted, shell variable expansions DO take place; in essence, the contents are treated by the shell like the contents of a double-quoted string.

      • 对于调试,使用 P W的,以便只的打印修改后的缓冲,没有写回文件。

      • For debugging, use ,p in place of w so as to only print the modified buffer, without writing it back to the file.

      [1] 重新就地更新:

      更多precisely, ED preserves该文件的现有的的inode 的,这保证了所有的的文件的属性是preserved。结果
      但是,它的的覆盖现有文件的单个字节,但这读取的全部的文件到缓冲存储器,并写入整个缓冲区该文件时问。结果
      这使得 ED 只适用于文件足够小,可以读入内存作为一个整体

      More precisely, ed preserves the file's existing inode, which ensures that all the file's attributes are preserved.
      However, it does not overwrite individual bytes of the existing file, but reads the entire file into a buffer in memory, and writes the entire buffer to the file when asked to.
      This makes ed suitable only for files small enough to be read into memory as a whole.

      相反, SED -i GNU 的和的 BSD SED ),它的 GNU 的4.1+对口, AWK -i就地 ,并且还 perl的-i 替换的原始文件具有的新创建有一个,这暗示她们:

      By contrast, sed -i (GNU and BSD sed), its GNU 4.1+ counterpart, awk -i inplace, and also perl -i replace the original file with a newly created one, which implies that they:


      • 破坏符号链接(!) - 如果输入文件是一个的符号链接的,它的具有相同名称的常规文件的替换

        • 的一种常见情况是重要的:说你的shell初始化文件〜/ .bashrc中符号链接的一个文件在其他地方,你不时源控制;然后安装使用的工具 SED -i 修改〜/ .bashrc中,这会导致它被替换成一个普通文件,并链接到你的源代码版本控制是坏了。

        • 更重要的是,BSD SED 的行为,甚至引入了安全风险(见下文)。

        • destroy symlinks(!) - if the input file was a symlink, it is replaced with a regular file of the same name
          • A common scenario where that matters: say your shell initialization file ~/.bashrc is a symlink to a file elsewhere you keep under source control; you then install a tool that uses sed -i to modify ~/.bashrc, which results in it being replaced with a regular file, and the link to your source-controlled version is broken.
          • What's more, BSD sed's behavior even introduces a security risk (see below).

          • 注意: BSD SED 引入了安全风险关于符号链接(行为仍present作为在FreeBSD 10)找到的版本:

            • 符号链接的的权限被复制到替换文件,而不是符号连接的的目标的的。由于符号链接得到的执行的默认权限,你总是最终获得的执行的文件,输入文件是否是可执行文件或没有。

            • Caution: BSD sed introduces a security risk with respect to symlinks (behavior still present as of the version found on FreeBSD 10):
              • The symlink's permissions are copied to the replacement file, not the symlink's target's. Since symlinks get executable permissions by default, you'll invariably end up with an executable file, whether the input file was executable or not.

              SED GAWK perl的 可以的解决这些问题上面采取额外的步骤,但有一件事,如果原来的inode保留,只能保证,如 ED

              sed, gawk, and perl could address the issues above by taking extra steps, but there's one thing that can only be ensured if the original inode is retained, as ed does:

              当正在更改按它的inode号监控(例如,用尾-f )的文件,而不是preserving的inode的打破了监控。

              When a file is being monitored for changes by its inode number (e.g., with tail -f), not preserving the inode breaks that monitoring.

              这篇关于用awk或sed来的声明钉在特定行的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆