sed - 使用具有单行的另一个文件的内容为文件的内容添加前缀
问题描述
我正在尝试将 a 的内容file1.txt
插入file2.txt
using sed
. 的内容file1.txt
只是一行,是一条路径。我希望将它作为前缀添加到每一行file2.txt
以及添加另一个/
字符。
$ cat file1.txt
/psot/rot8888/orce/db/tier/data/tine
$ cat file2.txt
o1_mf_users_abchwfg_.dbf
o1_mf_toptbs2_abchrq0_.dbf
o1_mf_toptbs1_abchrl2_.dbf
o1_mf_toptbs1_abchtlf_.dbf
所需的输出应如下所示:
/psot/rot8888/orce/db/tier/data/tine/o1_mf_users_abchwfg_.dbf
/psot/rot8888/orce/db/tier/data/tine/o1_mf_toptbs2_abchrq0_.dbf
/psot/rot8888/orce/db/tier/data/tine/o1_mf_toptbs1_abchrl2_.dbf
/psot/rot8888/orce/db/tier/data/tine/o1_mf_toptbs1_abchtlf_.dbf
试过的命令:
$ sed '/o1/ r file1.txt' file2.txt >> test.txt
$ cat test.txt
o1_mf_users_abchwfg_.dbf
/psot/rot8888/orce/db/tier/data/tine
o1_mf_toptbs2_abchrq0_.dbf
/psot/rot8888/orce/db/tier/data/tine
o1_mf_toptbs1_abchrl2_.dbf
/psot/rot8888/orce/db/tier/data/tine
o1_mf_toptbs1_abchtlf_.dbf
/psot/rot8888/orce/db/tier/data/tine
解决方案
您可以使用pr
它而不必担心sed
元字符、分隔符等。
$ cat ip.txt
abcd.xyz
123.txt
foo_baz.txt
$ cat f1
/a/b/c/d/
$ pr -mts"$(< f1)" /dev/null ip.txt
/a/b/c/d/abcd.xyz
/a/b/c/d/123.txt
/a/b/c/d/foo_baz.txt
where-m
允许并行粘贴文件,-s
是要合并的文件之间的分隔符。在这里,/dev/null
用作其中一个文件的虚拟对象,因为只有分隔符必须加上前缀。
如果您需要在包含前缀的文件内容之后添加更多字符:
$ cat ip.txt
abcd.xyz
123.txt
foo_baz.txt
$ cat f1
/a/b/c/d
$ pr -mts"$(< f1)"'/' /dev/null ip.txt
/a/b/c/d/abcd.xyz
/a/b/c/d/123.txt
/a/b/c/d/foo_baz.txt
推荐阅读
- android - 为什么我不能访问不在约束布局框架中的字段
- java - 如何从 javax.ws.rs.client.Entity 的实例中获取 HTTP 请求体(字符串)?
- angular - 处理多个 Angular 可观察对象
- api - 无法覆盖strapi中的自定义控制器
- django - 成功安装 psycop2 后,在我的 pycharm 终端中运行任何命令时导入 _psycopg 错误时出现 DLL 加载失败
- php - 关联多维(多维)PHP 数组如何转换为可下载的 CSV?
- ruby-on-rails - Github 操作和 RAILS_MASTER_KEY
- c - 是否可以从最新的 Android 版本中的类创建 NewGlobalRef?
- c# - 带有 Okta 的 Blazor 服务器端无法启动 - Okta 开发论坛一周后没有答案
- mysql - MySql Query 以随机是或否添加列