0

I'm working on creating bash script to read a CSV file (comma delineated). The file contains parts of names for files in another directory. I then need to take these names and use them to search the directory and copy the correct files to a new folder.

I am able to read the csv file. However, csv file only contains part of the file names so I need to use wildcards to search the directory for the files. I have been unable to get the wildcards to work within the directory.

CSV File Format (in notepad):

12
13
14
15

Example file names in target directory:

IXI12_asfds.nii
IXI13_asdscds.nii
IXI14_aswe32fds.nii
IXI15_asf432ds.nii

The prefix to all of the files is the same: IXI. The csv file contains the unique numbers for each target file which appear right after the prefix. The middle portion of the filenames are unique to each file.

#!/bin/bash
# CSV file with comma delineated numbers. 
# CSV file only contains part of the file name. Need to add IXI to the 
beginning, and search with a wildcard at the end. 

input="CSV_file.csv"
while IFS=',' read -r file_name1
do
   name=(IXI$file_name1)
   cp $name*.nii /newfolder

done < "$input"

The error I keep getting is saying that no folder with the appropriate name is able to be identified.

johnsmith
  • 1
  • 2
  • Post 4-5 example filenames. Also way `name=(IXI$file_name1)`?? That creates an array of 1 file named `"IXIthenamein-file_name1"`?? It is also critical to know **which part** of the filename is contained in the `csv`? The 1st part, the 2nd part, the middle part? Post a line from the `.csv` file as well. – David C. Rankin Jan 09 '18 at 18:55
  • 1
    I strongly urge you to use a proper CSV parser such as [csvtool](https://stackoverflow.com/a/1560498/68587) rather than reading it by hand. CSVs are complicated to parse if fields can be quoted, because then they may contain embedded commas, newlines, and other tricky characters. – John Kugelman Jan 09 '18 at 19:02
  • Updated the csv format as well as the number of files. The goal with the name=(IXI$file_name1) was to add the IXI to the number read from the csv file. The goal was to then search the directory using the wildcards to find and copy that file. Haven't done any Bash scripting previously so appreciate the help – johnsmith Jan 09 '18 at 19:30
  • Does `/newfolder` exist? – JNevill Jan 09 '18 at 19:43
  • Also, why set `IFS` to comma if `/n` is your record separator and there is only on column to deal with? – JNevill Jan 09 '18 at 19:44
  • The error is: cannot access '{IXI15'$'\r''.txt}': No such file or directory. As best I can tell it is forming the variable name with the different file prefixes. But when I try to copy on the next line the wildcard isn't working and identifying the files to copy. It seems like the wild card isn't actually being applied when i tested with ls function. Newfoldier s is available. @JohnKugelman – johnsmith Jan 09 '18 at 19:48
  • Looks like your CSV file has Windows line endings `\r\n` rather than UNIX ones `\n`. – John Kugelman Jan 10 '18 at 00:04

0 Answers0