AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 423659
Accepted
Herpes Free Engineer
Herpes Free Engineer
Asked: 2018-02-13 10:01:02 +0800 CST2018-02-13 10:01:02 +0800 CST 2018-02-13 10:01:02 +0800 CST

如何在模式之前将命令输出直接插入文件中?

  • 772

这个问题及其答案显示了现有文件(“processedFile”)的内容可以插入到另一个现有文件(“receivingFile”)中,然后再插入其中的一个模式,例如:

sed -i -e '/pattern/r processedFile' receivingFile

有时,此类内容可能来自先前的 command-unit,并且尚未写入。

那时,是否可以将命令单元的结果插入到接收文件中而不创建/写入已处理文件?

例子:

someFile比如说,我们进行如下预处理(从第 4 行开始读取其内容到文件末尾):

awk 'NR>3' someFile

现在,无需将其写入新文件,我们希望将其直接插入到 receivedFile 的模式之前,这样:

awk 'NR>3' someFile | <insertion commands>

PS:最好使用awkor sed。


根据@RomanPerekhrest 的要求,

一些文件:

# random output
# random
11
0 0 0 23.259058 2.1756592 -1.2097659 -0.074128056 0.34343502 -0.017659865
0 0 0.05 51.091269 4.8520157 -0.0011590485 0.00096592555 0.0059719715 -0.050598505

预处理:

awk 'NR>3' someFile:

0 0 0 23.259058 2.1756592 -1.2097659 -0.074128056 0.34343502 -0.017659865
0 0 0.05 51.091269 4.8520157 -0.0011590485 0.00096592555 0.0059719715 -0.050598505

接收文件:

stackExchange is the best.
pattern
Unix stackExchange is the best of the bests.
shell text-processing
  • 3 3 个回答
  • 1369 Views

3 个回答

  • Voted
  1. Best Answer
    A.B
    2018-02-13T10:46:27+08:002018-02-13T10:46:27+08:00

    我不知道它是否在所有 *NIX 上都是可移植的,具体取决于实现。它在我的 Linux Debian 上运行。

    TL;DR:/dev/stdin用作包含文件,这是 sed 的输入,将在管道之前获取命令的输出,例如

    awk 'NR>3' someFile | sed -i -e '/pattern/r /dev/stdin' receivingFile
    

    示例:目标文件内容称为receivingFile:

    stackExchange is the best.
    pattern
    Unix stackExchange is the best of the bests.
    

    输入样本称为someFile:

    # random output
    # random
    11
    0 0 0 23.259058 2.1756592 -1.2097659 -0.074128056 0.34343502 -0.017659865
    0 0 0.05 51.091269 4.8520157 -0.0011590485 0.00096592555 0.0059719715 -0.050598505
    

    在模式之后插入:

    awk 'NR>3' someFile | sed -e '/pattern/r /dev/stdin' receivingFile
    

    要在模式之前插入,请重用由 OP 链接的答案并转储到输出:

    awk 'NR>3' someFile | sed -n -e '/pattern/r /dev/stdin' -e 1x -e '2,${x;p}' -e '${x;p}' receivingFile
    

    要执行所要求的操作并替换文件:

    awk 'NR>3' someFile | sed -i -n -e '/pattern/r /dev/stdin' -e 1x -e '2,${x;p}' -e '${x;p}' receivingFile
    

    接收文件的新内容:

    stackExchange is the best.
    0 0 0 23.259058 2.1756592 -1.2097659 -0.074128056 0.34343502 -0.017659865
    0 0 0.05 51.091269 4.8520157 -0.0011590485 0.00096592555 0.0059719715 -0.050598505
    pattern
    Unix stackExchange is the best of the bests.
    

    更新:正如@don_crissti 所指出的,如果模式是最后一行,这将不起作用。这是另一个命令,改编自他对同一个链接问题的回答!需要一些其他命令来适应额外空白行的临时添加(和 sed 抑制)。这两个会起作用:

    awk 'NR>3' someFile | sed -e '/pattern/{r/dev/stdin' -e 'N;:l;$!n;$!bl};${/^$/!{s/\n$//};//d}' receivingFile <(printf '\n') > receivingFile.new
    mv receivingFile.new receivingFile
    

    或者只是(不需要 bash 并离开sed -i来处理最终文件):

    awk 'NR>3' someFile | { echo '' >> receivingFile; sed -i -e '/pattern/{r/dev/stdin' -e 'N;:l;$!n;$!bl};${/^$/!{s/\n$//};//d}' receivingFile ; }
    

    查看分行 sed 代码的链接答案。

    • 4
  2. Wildcard
    2018-02-13T15:36:25+08:002018-02-13T15:36:25+08:00

    只需使用exPOSIX 指定的脚本形式vi(也是 POSIX 指定的)。

    printf '%s\n' '/pattern/-r !awk "NR>3" somefile' x | ex receivingFile
    

    或更一般地说:

    printf '%s\n' '/pattern/-r !somecommand' x | ex somefile
    
    • 3
  3. ctac_
    2018-02-13T11:54:57+08:002018-02-13T11:54:57+08:00

    使用 sed (GNU sed) 4.4

    sed -i '/pattern/{h;s/.*/awk "NR>3" somefile/e;p;x}' receivingFile
    
    • 1

相关问题

  • 重新排列字母并比较两个单词

  • 在awk中的两行之间减去相同的列

  • 多行文件洗牌

  • 如何将带有〜的路径保存到变量中?

  • 如何更改字符大小写(从小到大,反之亦然)?同时[重复]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve