Q:

How to add a column at the end of multiple csv files using shell script

I have a couple of thousands CSV file. All of them have same structure and header. I would like to add a column at the end of the file. I found several solutions that add a column and value to that column but I didn't find anything that adds the header for that new column. For example, I have files like 1001.csv, 1002.csv, 1003.csv and so on. 
Contents of 1001.csv

ID,URL
1,one.com
2,two.com

I want to modify it like this
ID,URL,FILE
1,one.com,1001
2,two.com,1001

Since I have tons of files like this, I don't want to mess up the data while adding a column. Also, I don't want to produce extra files if it's possible to do in place update. 

A:

I tested this on a huge number of files and it worked really fast. This code removes the header first then add a column plus value to the column and finally brings the header back.   
#!/bin/bash

# How to run $ ./this-script.sh inputdir/
# here inputdir contains all csv files

# input argument is dir name
DIRNAME=`basename $1`
# go to target directory
cd $DIRNAME

# get list of all csv files
csvfiles=`ls *.csv`

for FILENAME in $csvfiles
do
    echo $FILENAME
    # filename without extension
    CODE="${FILENAME%.*}"
    echo $CODE

    ## remove header
    tail -n +2 "$FILENAME" > "$FILENAME.tmp" && mv "$FILENAME.tmp" "$FILENAME"

    ## add new field at the end
    sed "s/$/,$CODE/" "$FILENAME" > "$FILENAME.tmp2"

    ## add header with new column name
    # keep filename.bak as a backup for safety
    sed -i.bak 1i"id,url,file" "$FILENAME.tmp2"

    # if all good then remove temp files
    rm "$FILENAME"
    rm "$FILENAME.tmp2.bak"

    # rename output file to original name
    mv "$FILENAME.tmp2" "$FILENAME"     
done
# go back to parent directory
cd ..

