0

I have to automate simulations and to do so I need to create input files for each simulation. The majority of my simulations are almost identical, with one line of text changed from one file to the next. How can I take a text file, and make a number of copies of it with a specific line changed? For example if a text file held:

! input file
a = 6
b = 6
d = 789
! end

Say I wanted to create 6 new files from this template but my variable b is reduced by one in each subsequent file, how can I do this in either bash or python?

1

basic method can be similar to this in example i modify a= value bye numbers & files & filename also have value inside so it is separated files

#!/bin/bash


for i in a b c 1 2 3  ; do
    cat > file${i} << EOT
! input file
a = ${i}
b = 6
d = 789
! end
EOT
done

so you get 6 files with 6 different contents as :

# cat file?
! input file
a = 1
b = 6
d = 789
! end
! input file
a = 2
b = 6
d = 789
! end
! input file
a = 3
b = 6
d = 789
! end
! input file
a = a
b = 6
d = 789
! end
! input file
a = b
b = 6
d = 789
! end
! input file
a = c
b = 6
d = 789
! end

if you have to read b value from a reference file you can user a variable from read subcommand for example

while read ; do
cat > file${REPLY} << EOT
! input file
a = 1
b = ${REPLY}
d = 789
! end
EOT
done < referencefile

full example in real condition :

[root@h2g2w tmp]# cat > ./test.sh
while read ; do
cat > file${REPLY} << EOT
! input file
a = 1
b = ${REPLY}
d = 789
! end
EOT
done < referencefile


[root@h2g2w tmp]# cat > referencefile 
qsd
gfd
eza
vxcv
bxc
[root@h2g2w tmp]# 
[root@h2g2w tmp]# sh ./test.sh 
[root@h2g2w tmp]# ls -lrth file???
-rw-r--r--. 1 root root 41 28 juin  22:47 fileqsd
-rw-r--r--. 1 root root 41 28 juin  22:47 filegfd
-rw-r--r--. 1 root root 41 28 juin  22:47 fileeza
-rw-r--r--. 1 root root 41 28 juin  22:47 filebxc
[root@h2g2w tmp]# cat file???
! input file
a = 1
b = bxc
d = 789
! end
! input file
a = 1
b = eza
d = 789
! end
! input file
a = 1
b = gfd
d = 789
! end
! input file
a = 1
b = qsd
d = 789
! end
[root@h2g2w tmp]# 

I hope you can now adapt that to your needs.

  • I have read both this answer and glenn's, and I want to select the one that works best for my needs, so I will be attempting to implement these before I do so. Thank you very much. I especially like that I can write one bash script that contains the template to be copied. Thank you – Sinux1 Jun 28 '18 at 22:36
  • Quick question , if there is a second variable that needs to be changed also, from a different range of values, how can I incorporate that into your answer? Say that when a changes, so does b, but string values? – Sinux1 Jun 28 '18 at 23:12
0

With just bash: the file is named "file"

# slurp the whole file into a variable
contents=$(< file)

# find the value of b, using regular expression
# - with bash regex, the literal parts are quoted and the metachars are not
if [[ $contents =~ "b = "([0-9]+) ]]; then
    b_val=${BASH_REMATCH[1]}

    # create a printf-style format string
    template=${contents/b = $b_val/b = %d}

    # then, count down from the b value to 1, creating the new files
    for (( n = b_val; n >= 1; n-- )); do
        printf "$template\n" $n > "file.$n"
    done
fi

Then:

$ ls -lt
total 6220
-rw-rw-r-- 1 jackman jackman      39 Jun 28 17:46 file.1
-rw-rw-r-- 1 jackman jackman      39 Jun 28 17:46 file.2
-rw-rw-r-- 1 jackman jackman      39 Jun 28 17:46 file.3
-rw-rw-r-- 1 jackman jackman      39 Jun 28 17:46 file.4
-rw-rw-r-- 1 jackman jackman      39 Jun 28 17:46 file.5
-rw-rw-r-- 1 jackman jackman      39 Jun 28 17:46 file.6
-rw-rw-r-- 1 jackman jackman      39 Jun 28 17:35 file
...

$ grep ^b file{,.?}
file:b = 6
file.1:b = 1
file.2:b = 2
file.3:b = 3
file.4:b = 4
file.5:b = 5
file.6:b = 6

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.