如何在bash脚本的CSV的下一列中添加文本

问题描述 投票:0回答:1

我有2个文件

#cat file.txt
aaa
bbb
ccc

#cat input.txt
191
112
339

我需要

#cat output
aaa,191
bbb,112
ccc,339

我正在尝试

while IFS= read -r line
do

cat file.txt | grep "\.$line$" | wc -l >> output.txt
sed -i "1 i\\$line"  output.txt

done < input.txt

但是它会打印组中每个文件的所有行。

知道如何存档结果吗?

bash loops sed while-loop paste
1个回答
0
投票

如果您是喜欢写多行而不是少行的类型,可以尝试以下方法:

echo -n "" > output.txt
while IFS= read -r line_input && IFS= read -r line_file <&3
  do
    echo "$line_input,$line_file" >> output.txt
done <input.txt 3<file.txt

如果没有,您可以简单地使用@ RavinderSingh13提示;-)

© www.soinside.com 2019 - 2024. All rights reserved.