1

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

run_mcmc=1

n_samples=1000
n_thin=100
nu=3.
mu=1.
sig=1.
start_data_file=\"data_file
end_data_file=.txt\"
seed=1
alpha=1.


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

    data_file=$start_data_file$num$end_data_file
    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 
        ./student_t_sampler_copy 
    fi

    n_bins=100
    # 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 
done

wait # wait for all processes to finish

The output is as follows

"data_file1.txt"
gcc -I/usr/include -L/usr/local/lib  student_t_sampler_copy.c -o                    student_t_sampler_copy -lgsl -lgslcblas -lm
"data_file2.txt"
gcc -I/usr/include -L/usr/local/lib  student_t_sampler_copy.c -o student_t_sampler_copy -lgsl -lgslcblas -lm
"data_file3.txt"
gcc -I/usr/include -L/usr/local/lib  student_t_sampler_copy.c -o student_t_sampler_copy -lgsl -lgslcblas -lm
"data_file4.txt"
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
0

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.