Friday, October 28, 2022

[SOLVED] Insert filename as column, separated by a comma

Issue

I have 100 file that looks like this

>file.csv
gene1,55
gene2,23
gene3,33

I want to insert the filename and make it look like this:

file.csv
gene1,55,file.csv
gene2,23,file.csv
gene3,33,file.csv

Now, I can almost get there using awk

awk '{print $0,FILENAME}' *.csv > concatenated_files.csv

But this prints the filenames with a space, instead of a comma. Is there a way to replace the space with a comma?


Solution

Is there a way to replace the space with a comma?

Yes, change the OFS

$ awk -v OFS="," '{print $0,FILENAME}' file.csv
gene1,55,file.csv
gene2,23,file.csv
gene3,33,file.csv


Answered By - HatLess
Answer Checked By - David Marino (WPSolving Volunteer)