I have two text files of the same length, in order: one of products, the other of comparison products. I'd like the output to be "product;comparison" for each line. Yes I could throw them in a spreadsheet and export as csv, but I'd like to understand how to do it in bash. The two files are supplied as arguments.
#!/bin/bash
file1="$1";
file2="$2";
separator=";";
while read -r product
do
read -r comp < "$file2";
echo $product $separator $comp >> temp.txt;
done < "$file1"
tr -d " " < temp.txt | sort > out.txt
rm temp.txt
This gives me all the products with the same comparison product! Eg
$: cat out.txt
product1;comp1
product2;comp1
product3;comp1
I'm obviously not reading the single line of the comp file properly – what am I doing wrong? How do I read a single line?
Best Answer
If you really want to do it in bash, I'd suggest making use of the
-u fd
option to read from the two files simultaneously using numbered streams, e.g.However, you could just use the
paste
utility