Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here File1 is the template. File2 is a csv with 200 rows with the columns scriptname ,jobname, templatename, location
The templatename is column with in the file2.

Is it possible to do it as below. The purpose is to use different templates (file1's) for different rows in file2. Any help is appreciated
$templatename I want to change with rows of flie 2. Is it possible? The following is not working:

  • samplefile1


statments go here

change jobname

save in the location folder (already there)

  • sample file2

scriptname, jobname, templatename, location

qqqtest,sew-test,/san00/test/file1, /san00/test/

qqqtest,sew-test,/san00/test/file2, /san00/test/

What I am expecting is to change the file1, file2 templates etc. each time with the below update by reading csv?

while IFS=, read scriptname jobname templatename location; 
do sed -e "s/scriptname/$scriptname/" \
  -e "s/jobname/$jobname/" $templatename > "$location" 
done < (file2) 

How I update the $templatename?

share|improve this question

I'm not sure I understand what you want, but I can see several things that probably need fixing.

IFS=, read scriptname jobname templatename location reads comma-separated values. In file2, you seem to have whitespace around commas sometimes. The whitespace will end up in the variables. If there is never any whitespace inside a field, set IFS=', ' to treat any sequence of one comma surrounded by optional whitespace or any whitespace sequence as a separator. If there can be whitespace inside fields but not at the beginning or end, I recommend preprocessing file2 first:

<file2 sed -e 's/^  *//' -e 's/ *, */,/g' -e 's/  *$//' | while IFS=, read …; do …; done

By the way, <(file2) is a syntax error in some shells, and in other shells it means that file2 is a command to be executed (process substitution). You meant <file2.

If there can be a / in the script name or the job name, it will end up inside the sed command and cause a sed syntax error. Since you cannot have a comma in any of the fields of file2 by construction, use , as the sed s command separator:

sed -e "s,scriptname,$scriptname," -e 's,jobname,$jobname,' …

In your example data, the location is a directory, but in your code, you try to write output to it. If the location is a directory, you need to supply a file name. If you want to use the same file name as $templatename when the location is a directory, you can do this:

if [ -d "$location" ]; then

The files $location and $templatename cannot be the same file, otherwise sed … "$templatename" >"$location" will first truncate that file and then start reading from it. To allow for the possibility that the files are the same, write the sed output to a temporary file, then move it into place. If you have mkstemp:

tmp=$(TMPDIR=$location_dir mktemp -t)
sed … >"$tmp"
mv -f -- "$tmp" "$location"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.