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

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published