Search code examples
bashshellcsvterminalcut

Removing Columns From ALL CSV Files in Specific Folder Then Outputting File With Date


I'm trying to automate some processes by using hazel app to move a file to a specific folder, execute a shell script on any csv in that folder, and then move it to another folder. Right now the part I'm working on is the shell script. I have been testing out the cut command in terminal on csvs but I'm not sure if its the same thing as a shell script since it doesnt seem to be working but what I have is:

cut -d',' -f2,12 test.csv > campaigns-12-31-13.csv

It looks for test.csv but I would like it to work with any csv, and it also exports it with the date 12-31-13 but I'm just trying to get it to export with whatever yesterdays date was.

How do I convert this to a shell script that will execute on any csv in the folder and so it adds the date for yesterday at the end of the filename?


Solution

  • You can try the following script:

    #! /bin/bash
    
    saveDir="saveCsv"
    dd=$(date +%Y-%m-%d -d "yesterday")
    for file in *.csv ; do
        bname=$(basename "$file" .csv)
        saveName="${saveDir}/${bname}-${dd}.csv"
        cut -d',' -f2,12 "$file" > "$saveName"
    done