Copy n lines from one CSV and paste them n times in another CSV

I'm having a bit of a problem trying to figure this one out.

I have two CSV files, one of them without headers and the other with headers.

file1.csv (no headers):
apples
bananas

file2.csv (with headers):
HEADER1,HEADER2
item1,item3
item1,item3
item1,item3
item2,item3
item2,item3
item2,item3

I would like to create a new file: "file3.csv" where "apples" and "bananas" are listed the same number of times as "item1" and "item2" respectively.

The resulting file3.csv should look like this:

file3.csv
HEADER1,HEADER2,HEADER3
item1,item3,apples
item1,item3,apples
item1,item3,apples
item2,item3,bananas
item2,item3,bananas
item2,item3,bananas

To add the new header I'm using echo "$(head -n 1 file2.csv),HEADER3 >file3.csv.

To copy and paste the lines I've tried to do it with: awk -F, -v OFS=, 'NR==FNR {a[$1]=$1; next} {print $0,a[$1]}' file1.csv file2.csv but it doesn't work as I intend.

If an awk expert could help me a little, I would be eternally thankful.

Thanks!



from Recent Questions - Stack Overflow https://ift.tt/3kX6DRa
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)