Create multiple files

dear all

suppose I have two files file_000 and file_id:

file_000:

blablablabla000blablabla000
000blahblah000blahblah
blah000blahblah

file_id:

001
002
003

now, based on file_id, I want to create 3 files; the name of each file would be file_001,file_002,file_003,respectively, and the contents of each file would be identical to that of file_000, except all "000" would be replaced with file_id.

For example, file_001 would be:

file_001:

blablablabla001blablabla001
001blahblah001blahblah
blah001blahblah

My question is: what is the quickest way to make those 3 files? is it possible to just use a one-liner?

Thank you very much.

#!/bin/ksh

file="file_000"

while read id
do
        cp "$file" "${file%_*}_$id"
done < file_id
1 Like

Try:

awk 'NR==FNR{a[NR]=$0;n=NR;next}{for (i=1;i<=n;i++){x=a;gsub("000",$0,x);print x >> "file_"$0}}' file_000 file_id
1 Like

Slight change to Yoda's solution to replace ID within file.

Note this should also work find with /bin/bash (if you don't have ksh installed).

#!/bin/ksh

file="file_000"

while read id
do
    sed 's/000/'"$id"'/g' "$file" > "${file%_*}_$id"
done < file_id
1 Like

Thank you all for help!