For my script I have to read the data from a file.

For example I have 2 files: one is a text file, the second is a csv file with 10 columns.

What I am trying to do is to read the csv and change the parameters in the first file.

  1. Using sed -n to read the data one row at a time and save this in a fileC
  2. Using sed "s/&replacefile1param/&readFileC/g"

What I have issue is to read the cat of readFileC as a parameter in the second step. How can I do this?

The first file is a sample script file with 2 parameters inside. The second file is a CSV file with 200 lines with 6 columns.

I need to change the template file by reading the columns from the CSV, and substituting the parameters. The result need to be 200 file1 and each result needs to be saved in the location specified in the the 4th column of the CSV as well.

Sample template file

The name =scriptname

The staments go here

job name = jobname


Sample CSV data


I need to replace the parameters in the template file with values taken from the CSV, and save the file in the /san00 location from the 4th column of the CSV.

Desired output

The script name kkkkkk

The staments go here

job name = filetest


This should be saved in san00/project.

  • 5
    Could you post a short piece of sample data and show exactly what the desired output is? (I don't really understand exactly what you're trying to do.)
    – Mat
    Mar 3, 2012 at 15:30
  • file1 = i play <football> file2= teenis, date, location
    – math
    Mar 3, 2012 at 16:45
  • 1
    Edit your question (there's a link just below the tags) to add the additional information, don't put it in comments.
    – Mat
    Mar 3, 2012 at 16:46
  • @math Even with your comment I don't understand what you want to do. Please edit your question and show an example of file1 contents, file2 contents, and desired output. Mar 3, 2012 at 23:40

2 Answers 2


file1 is the template and file2 the csv. What happens if two rows have the same location, is undefined, the solution below overwrites the location file with the latest data. It is also unclear to me if the /san00 directory is the only one used in the location part and if it is supposed to be an absolute path.

Solution creating locations relative to current directory (notice the . in .$dir and .$location).

while IFS=, read scriptname jobname date location updated env; do
    dir=$(dirname $location)
    [ -d .$dir ] || mkdir -p ".$dir"
    sed -e "s/scriptname/$scriptname/" \
        -e "s/jobname/$jobname/" file1 > ".$location"
done < <(tail -n +2 file2)

Solution with absolute paths in location, target directories must pre-exist

while IFS=, read scriptname jobname date location updated env; do
    sed -e "s/scriptname/$scriptname/" \
        -e "s/jobname/$jobname/" file1 > "$location"
done < <(tail -n +2 file2)

There are many ways to extract the information of file2, the above is just one of them, and depending on the nature of the data it might be more complicated than this.

  • Thank you for the answer. Here File1 is the template. IF I have the templatename with in the file2 is it possible to do it as below. The purpose is to use different template for different rows in file2. Any help is appreciated while IFS=, read scriptname jobname templatename date location updated env; do sed -e "s/scriptname/$scriptname/" \ -e "s/jobname/$jobname/" $templatename > "$location" done < (file2)
    – math
    Mar 7, 2012 at 20:45

This might work for you:

sed '1d;s|\([^,]*\),\([^,]*\),[^,]*,\([^,]*\).*|sed "1s/scriptname/\1/;5s/jobname/\2/" template >\3|' file.csv |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .