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

Her*_*eer 5 shell sed awk text-processing

这个问题及其答案显示现有文件(“processedFile”)的内容可以在其模式之一之前插入到另一个现有文件(“receivingFile”)中,这样:

sed -i -e '/pattern/r processedFile' receivingFile
Run Code Online (Sandbox Code Playgroud)

有时,此类内容可能来自先前的命令单元,并且尚未写入。

那时,是否可以将命令单元的结果插入到receiveFile中而不创建/写入processedFile?

例子:

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

awk 'NR>3' someFile
Run Code Online (Sandbox Code Playgroud)

现在,无需将其写入新文件,我们希望将其直接插入到receivingFile中的模式之前,例如:

awk 'NR>3' someFile | <insertion commands>
Run Code Online (Sandbox Code Playgroud)

PS:最好使用awksed


根据@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
Run Code Online (Sandbox Code Playgroud)

预处理:

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
Run Code Online (Sandbox Code Playgroud)

接收文件

stackExchange is the best.
pattern
Unix stackExchange is the best of the bests.
Run Code Online (Sandbox Code Playgroud)

A.B*_*A.B 5

我无法判断它是否可以跨所有 *NIX 移植,具体取决于实现。它在我的 Linux Debian 上运行。

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

awk 'NR>3' someFile | sed -i -e '/pattern/r /dev/stdin' receivingFile
Run Code Online (Sandbox Code Playgroud)

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

stackExchange is the best.
pattern
Unix stackExchange is the best of the bests.
Run Code Online (Sandbox Code Playgroud)

输入样本名为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
Run Code Online (Sandbox Code Playgroud)

要在模式后插入:

awk 'NR>3' someFile | sed -e '/pattern/r /dev/stdin' receivingFile
Run Code Online (Sandbox Code Playgroud)

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

awk 'NR>3' someFile | sed -n -e '/pattern/r /dev/stdin' -e 1x -e '2,${x;p}' -e '${x;p}' receivingFile
Run Code Online (Sandbox Code Playgroud)

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

awk 'NR>3' someFile | sed -i -n -e '/pattern/r /dev/stdin' -e 1x -e '2,${x;p}' -e '${x;p}' receivingFile
Run Code Online (Sandbox Code Playgroud)

接收文件的新内容:

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.
Run Code Online (Sandbox Code Playgroud)

更新:正如@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
Run Code Online (Sandbox Code Playgroud)

或者只是(不需要 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 ; }
Run Code Online (Sandbox Code Playgroud)

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