Bash:在模式之后插入一个文件的内容到另一个文件中

Bash:在模式之后插入一个文件的内容到另一个文件中,第1张

概述Bash:在模式之后插入一个文件的内容到另一个文件中

我想写一个bash脚本,它将执行以下 *** 作:

从第一个文件读取内容(作为第一个参数)

从第二个文件读取内容(作为第二个参数)

在第二个文件中find具有给定模式的行(作为第三个参数)

将文本从第一个文件插入到第二个文件的行之后。

在屏幕上打印最终文件。

例如:

first_file.txt:

111111 1111 11 1

second_file.txt:

区分哪些文件夹丢失

git for windows的包pipe理?

是否可以只grep只有一列,并同时打印其他SELECTED输出

打击文件 *** 作

如何通过在bash中使用xargs命令来检查版本?

122221 2222 22 2

模式:

2222

输出:

122221 111111 1111 11 1 2222 111111 1111 11 1 22 2

我应该怎样在BASH上实现这个function?

我写了代码,但它不能正常工作(为什么?):

#!/bin/bash first_filename="$1" second_filename="$2" pattern="$3" while read -r line do if [[ $line=˜$pattern ]]; then while read -r line2 do echo $line2 done < $second_filename fi echo $line done < $first_filename

无法创build备份文件(添加!覆盖)

shell脚本中的三重嵌套引用

在shell脚本中删除文件中的certan行的第一个字符

Shell脚本在成功执行后离开进程

linux Bash脚本 – 将参数中的小写path与实际的文件系统path相匹配

在=~运算符周围需要空格。 比较:

[[ foo=~bar ]] [[ foo =~ bar ]]

这是因为第一个表达式本质上是“这个字符串是空的吗?

另外, OP代码使用小的代字符而不是代字号 。

即使如此,你也可以很容易地摆脱内在的循环。 只需用cat -- "$second_filename" while read -r line2位来替换整个cat -- "$second_filename" 。

如果文件没有以换行符结尾(标准的* nix工具),那么最后的echo $line才是正确的。 相反,你应该使用, while read -r line || [[ $line ~= '' ]] while read -r line || [[ $line ~= '' ]] 。 这可以在最后使用或不使用换行。

此外, 使用更多行情™ 。

sed可以做到没有循环。 使用它的r命令:

sed -e '/pattern/rfile1' file2

测试环节:

$ cd -- "$(mktemp -d)" $ printf '%sn' 'nuts' 'bolts' > first_file.txt $ printf '%sn' 'foo' 'bar' 'baz' > second_file.txt $ sed -e '/bar/r./first_file.txt' second_file.txt foo bar nuts bolts baz

也使用awk工程。

要在###标记###行之前插入:

// for each <line> of second_file.txt : // if <line> matches regexp ###marker###,outputs first_file.txt. // **without any condition :** print <line> awk '/###marker###/ { system ( "cat first_file.txt" ) } { print; } ' second_file.txt

在###标记###行之后插入:

// for each <line> of second_file.txt : // **without any condition :** print <line> // if <line> matches regexp ###marker###,outputs first_file.txt. awk '{ print; } /###marker###/ { system ( "cat first_file.txt" ) } ' second_file.txt

要替换###标记###行:

// for each <line> of second_file.txt : // if <line> matches regexp ###marker###,outputs first_file.txt. // **else**,print <line> awk '/###marker###/ { system ( "cat first_file.txt" ) } !/###marker###/ { print; }' second_file.txt

如果要进行就地替换,请使用临时文件来确保在awk读取整个文件之前管道未启动; 加:

> second_file.txt.new mv second_file.txt{.new,} // (like "mv second_file.txt.new second_file.txt",but shorter to type !)

如果你想在行内进行替换(只替换模式并保留行的其余部分),应该用sed而不是awk来实现类似的解决方案。

这应该工作:

perl -lne 'BEGIN{open(A,"first_file.txt");@f=<A>;}print;if(/2222/){print @f}' second_file.txt

测试:

> cat temp 111111 1111 11 1 > cat temp2 122221 2222 22 2 > perl -lne 'BEGIN{open(A,"temp");@f=<A>;}print;if(/2222/){print @f}' temp2 122221 111111 1111 11 1 2222 111111 1111 11 1 22 2 >

我这样使用sed,它起到了魅力的作用

sed -i -e'/ pattern / r filetoinsert'文件被插入

它所做的是将'filetoinsert'插入到'filetobeinserted'后面的指定模式的行之后

小心选择一个独特的模式,不知道它将如何与一个重复的模式,我认为它会做的只是第一个

总结

以上是内存溢出为你收集整理的Bash:在模式之后插入一个文件的内容到另一个文件中全部内容,希望文章能够帮你解决Bash:在模式之后插入一个文件的内容到另一个文件中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1279368.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存