0

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?

Workhorse
  • 1,500
  • 1
  • 17
  • 27

3 Answers3

1

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
HatLess
  • 10,622
  • 5
  • 14
  • 32
0

Figured it out, turns out:

for d in *.csv; do (awk '{print FILENAME (NF?",":"") $0}' "$d" > ${d}.all_files.csv); done

Works just fine.

Workhorse
  • 1,500
  • 1
  • 17
  • 27
  • 1
    The parentheses are useless. there is no reason to run Awk in a subshell. – tripleee Oct 27 '22 at 15:43
  • 1
    this doesn't print `file.csv` on the first line (as displayed in expected output); this prints the filename at the beginning of each data line but in the expected output the filename is at the end of the data tlines – markp-fuso Oct 27 '22 at 16:20
0

You can also create a new field

awk -vOFS=, '{$++NF=FILENAME}1' file.csv
gene1,55,file.csv
gene2,23,file.csv
gene3,33,file.csv
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134