How to move files to specific folders based on CSV entries?

1

I have a CSV file, comma delineated. I need to move the file, listed in column 3 into the folder listed in column 12. I output both columns to different text files using:

awk -F ',' '{print $3}' /Users/amermels/Documents/MERGED/Sling\ Manifest\ 2-17-2017.csv > ../files.txt

awk -F ',' '{print $12}' /Users/amermels/Documents/MERGED/Sling\ Manifest\ 2-17-2017.csv > ../dates.txt

Then I tried:

for IX in 'ls' ; do mv -i `cat ../files.txt` `cat ../dates.txt` ; done

But it just doesn't work that way.

I'm sure this would be simple if I was more than a novice, but I just can't figure this out.

Example:

192543,FYI,BuyingTheView_SunsetCondoToronto_192543_SLING.xml,1_27_17 16:32,"2,299",,Yes,2017-01-30,2017-03-05,1_30_17,3_5_17

BuyingTheView_SunsetCondoToronto_192543_SLING.xml is the file and 3_5_17 is the folder.

NOTE: I used single quote ' instead of tick in my post due to the formatting of the page.

Alex L. Mermelstein

Posted 2017-02-17T23:57:14.700

Reputation: 11

You can use <pre> tags to include backticks as appropriate; I have edited your post with an example of that- please change or revert my edit if that conflicts with your intent. – bertieb – 2017-02-18T13:28:04.507

Answers

2

How can I move files based on CSV content?

Summary: Loop over the CSV itself and extract the filename and directory fields using cut.

I can see why you started in the direction you did; but rather than splitting the filename and directory into separate files, you can loop over the CSV file itself. Based on that, and putting a few things like the field numbers and CSV file path in variables in case they change at some point, you could do something along the lines of the following:

#!/bin/bash
# move_csv.sh - move XML files based on fields in a CSV 

filename_field=3
destination_field=12
csvfile="/Users/amermels/Documents/MERGED/Sling\ Manifest\ 2-17-2017.csv"

while read csv_line; do
    mv "`echo $csv_line | cut -d',' -f$filename_field`" "`echo $csv_line | cut -d',' -f$destination_field`"
    # eg mv "BuyingTheView_SunsetCondoToronto_192543_SLING.xml" "3_5_17"
done < $csvfile

(slightly quick and dirty; there's probably more idiomatic ways of extracting the fields from the line for mv rather than using two separate subprocesses and invocations of cut, but this at least works in my quick test)

Example 'output' (based on made up data similar in format to what you have):

mv "BuyingTheView_SunsetCondoToronto_192543_SLING.xml" "3_5_17"
mv "BuyingTheView_SunriseCondoToronto_192544_SLING.xml" "3_5_18"
mv "BuyingTheView_LakeviewCondoToronto_192545_SLING.xml" "3_5_18"
mv "BuyingTheView_SunsetCondoVancouver_192546_SLING.xml" "3_5_19"
mv "BuyingTheView_SunsetCondoOttawa_192547_SLING.xml" "3_5_20"
mv "BuyingTheView_SunsetApartmentMontreal_192548_SLING.xml" "3_5_20"

(This isn't really output; these are the mv commands that would be executed as the script looks through the CSV.)

Caution: pay attention to what directory this script is operating in! It assumes that the XML files exist in the working directory (where the script is running from) and the subdirectories already exist. If this is not the case it is relatively trivial to add either a cd to the correct working directory or use a base directory to add to the paths in the mv command

bertieb

Posted 2017-02-17T23:57:14.700

Reputation: 6 181

Nothing happens when I try and run your script. – Alex L. Mermelstein – 2017-02-18T21:36:05.107

@AlexL.Mermelstein How are you trying to run the script? Did you take note of the caveats about working directory etc? The script doesn't print any output; it should move the files into the specified directories nonetheless. OTOH you could add eg echo "mv '$(echo $csv_line | cut -d',' -f$filename_field)' '$(echo $csv_line | cut -d',' -f$destination_field)'" to the loop to print what it's doing. I don't know your exact setup so can't replicate it here! – bertieb – 2017-02-19T02:52:32.013

0

In your example, 192543,FYI,Buy...,1_...,"2,299",,Yes,2017...,2017...,1_...,3_5_17 the fifth field ("2,299") has a comma in it. My guess is that if this field is under 1,000 there would be no comma. Therefore, only 11 fields instead of 12. You could try:

awk -F, '{print $3 " " $NF}' /Users/amermels/Documents/MERGED/Sling\ Manifest\ 2-17-2017.csv | xargs -n 2 mv {} {}

bstipe

Posted 2017-02-17T23:57:14.700

Reputation: 111