首页 > 解决方案 > 如何使用 Sed 命令在最后一句的末尾添加一个文件

问题描述

我需要以 csv 格式输出 bash 脚本,我想要的输出如下。虽然我需要在 1.2.6.1.txt 的尾部之后附加名为 1.2.6.2.txt 的文件并且它不能是下一行条目,但最好使用 sed 命令完成此操作。

#cat result/1.2.6.1.txt
1.2.6 Disable Proxy Modules,command to get result httpd -M | grep proxy_,expected result should be null

和:

#cat result/1.2.6.2.txt
" proxy_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 proxy_connect_module (shared)
 proxy_express_module (shared)
 proxy_fcgi_module (shared)
 proxy_fdpass_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_scgi_module (shared)
 proxy_wstunnel_module (shared)"

我需要的输出:

1.2.6 Disable Proxy Modules,command to get result httpd -M | grep proxy_,expected result should be null,"
proxy_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)
"

已经尝试过此命令以在文件尾部之后附加文本

sed -e $'$s/$/:SUFFIX &/' 结果/1.2.6.1.txt

1.2.6 禁用代理模块命令以获取结果 httpd -M | grep proxy_ 预期结果应为 null:SUFFIX

但是当我尝试下面的命令时,假设在原句后添加 1.2.6.2.txt 内容的 e cat result/1.2.6.2 命令不起作用:

# sed -e $'$s/$/{e cat result\/1.2.6.2.txt}/' result/1.2.6.1.txt
1.2.6 Disable Proxy Modules
command to get result httpd -M | grep proxy_
expected result should be null{e cat result/1.2.6.2.txt}

标签: linuxbashappend

解决方案


您收到该错误是因为您需要转义/. {e cat result/1.2.6.2.txt}所以你可以按如下方式运行它:

sed -e $'s/$/{e cat result\/1.2.6.2.txt}/' result/1.2.6.1.txt

不使用sed,您可以简单地运行:

cat result/1.2.6.1.txt >> 1.2.6.2.txt


推荐阅读