I am iterating over indexes which are used to create data file id's. When I create the file name (data_file) and echo it, it prints properly, but when I try to sed that variable into the desired files it only places the first index.

# copy files
cp student_t_sampler.c student_t_sampler_copy.c
cp post_process.py post_process_copy.py



for i in "10. 1" "1. 2" "0.1 3" "0.01 4"; do # i is alpha, num
    IFS=' ' read -a myarray <<< "$i"

    echo $data_file
    if [ $run_mcmc == 1 ]; then 

        sed -i "s/nnn/$n_samples/g" student_t_sampler_copy.c # set number of samples, 1000
        sed -i "s/ttt/$n_thin/g" student_t_sampler_copy.c # set thinning, 5
        sed -i "s/nununu/$nu/g" student_t_sampler_copy.c # set nu
        sed -i "s/mumumu/$mu/g" student_t_sampler_copy.c # set mu
        sed -i "s/sigsigsig/$sig/g" student_t_sampler_copy.c # set sig
        sed -i "s/fff/$data_file/g" student_t_sampler_copy.c # data file
        sed -i "s/sss/$seed/g" student_t_sampler_copy.c # set seed
        sed -i "s/aaa/$alpha/g" student_t_sampler_copy.c # set alpha

        make ./student_t_sampler_copy 

    # echo "$data_file"
    sed -i "s/fff/$data_file/g" post_process_copy.py # set data file in python
    sed -i "s/bbb/$n_bins/g" post_process_copy.py # set number of bins
    sed -i "s/aaa/$alpha/g" post_process_copy.py # set set in plot title
    sed -i "s/iii/$num/g" post_process_copy.py # set set in plot title

    python post_process_copy.py 

wait # wait for all processes to finish

The output is as follows

gcc -I/usr/include -L/usr/local/lib  student_t_sampler_copy.c -o                    student_t_sampler_copy -lgsl -lgslcblas -lm
gcc -I/usr/include -L/usr/local/lib  student_t_sampler_copy.c -o student_t_sampler_copy -lgsl -lgslcblas -lm
gcc -I/usr/include -L/usr/local/lib  student_t_sampler_copy.c -o student_t_sampler_copy -lgsl -lgslcblas -lm
gcc -I/usr/include -L/usr/local/lib  student_t_sampler_copy.c -o    student_t_sampler_copy -lgsl -lgslcblas -lm

So even though it echos the correct data file, both python and c only receive data_file1.txt

  • I think you need to explain what you mean by "both python and c only receive data_file1.txt" – Kusalananda Jul 1 '16 at 6:09
  • why run sed so many times? sed can take multiple -e options, and/or you can have multiple commands in a quoted sed script separated by semi-colons or newlines. e.g. sed -i " s/nnn/$n_samples/g; s/ttt/$n_thin/g; ..." student_t_sampler_copy.c. BTW, you would be much better off by just improving both student_t_sampler.c and post_process.py so that they took command-line arguments for the variables you're changing - using sed as a substitute for getopt is a lot more complicated and prone to mistakes. – cas Jul 3 '16 at 4:50

You're producing the student_t_sampler_copy.c from the template student_t_sampler.c at the start of the script and outside the loop that updates the copy, therefore you're only updating the copy in the first iteration of the loop, and all of the other iterations there's nothing else to change. You have to copy the file within the loop (immediately after the for).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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