I am very new to Bash scripting and still at the learning stage. Currently, I am working on CSV files and there has been a requirement that if 2 rows are matched in 2 different CSV files, then the data column should be added.
For example,
csv_file1.csv:
Account,Entity,Data
A1101,E101,1000
csv_file2.csv:
Account,Entity,Data
A1101,E101,2000
A1101,E102,2000
On executing bash script, the expected output should be a new CSV file with rows:
Account,Entity,Data
A1101,E101,3000
A1101,E102,2000
Since, elements in 1st row of “csv_file1” and elements in 1st row of “csv_file2” are matching, data in the “Data” column should append.
The below is the code which I have tried to print the contents of both CSV files. But, I am not able to move forward from here.
#!/bin/bash
#assigns the CSV file to a variable
FILE1="csv_file1.csv"
#reads each line and splits it into three variables
while IFS=, read -r Account Entity Data; do
echo "$Account#""$Entity#""$Data"
done < <(tail -n +2 $FILE1)
#assigns the CSV file to a variable
FILE2="csv_file2.csv"
#reads each line and splits it into three variables
while IFS=, read -r Account Entity Data; do
echo "$Account#""$Entity#""$Data"
done < <(tail -n +2 $FILE2)
It would be great if you guys can help me with your ideas. please let me know if any additional information is needed. Thanks a lot in Advance!
1