Tux*_*ife 22 bash perl sed awk text-processing
猫文件1
foo
ice
two
Run Code Online (Sandbox Code Playgroud)
猫文件2
bar
cream
hundred
Run Code Online (Sandbox Code Playgroud)
期望的输出:
foobar
icecream
twohundred
Run Code Online (Sandbox Code Playgroud)
在我的场景中,file1 和 file2 将始终具有相同数量的行,以防事情变得更容易。
ste*_*ver 35
这项工作的正确工具可能是 paste
paste -d '' file1 file2
Run Code Online (Sandbox Code Playgroud)
详情请参阅man paste。
您也可以使用以下pr命令:
pr -TmJS"" file1 file2
Run Code Online (Sandbox Code Playgroud)
在哪里
-T 关闭分页-mJ 米二哥文件,Ĵ oining实线-S"" 用空字符串分隔列 如果您真的想使用纯 bash shell(不推荐)来做到这一点,那么这就是我的建议:
while IFS= read -u3 -r a && IFS= read -u4 -r b; do
printf '%s%s\n' "$a" "$b"
done 3<file1 4<file2
Run Code Online (Sandbox Code Playgroud)
(仅包含此内容是因为该主题出现在对另一个提议的纯 bash 解决方案的评论中。)
通过awk方式:
awk '{getline x<"file2"; print $0x}' file1
Run Code Online (Sandbox Code Playgroud)
getline x<"file2"从file2读取整行并保存到x变量中。print $0x使用then打印file1 中的整行,这是file2的保存行。$0x