gpt4 book ai didi

sed - 使用具有单行的另一个文件的内容作为文件内容的前缀

转载 作者:行者123 更新时间:2023-12-04 09:36:56 24 4
gpt4 key购买 nike

我正在尝试插入 file1.txt 的内容进入 file2.txt使用 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
哪里 -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

关于sed - 使用具有单行的另一个文件的内容作为文件内容的前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62529075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com