I have file 1 which contains:


I have file 2 which contains:

11111 22222 33333

and file 3 which contains:

22222 11111 33333

I want to insert each line in file 1 into file 2 and 3 replacing '11111' and creating a new file each time. So the end result is 4 files:

File 1:


File 2:


File 3:


File 4:


I have attempted to use a nested for loop and sed to replace the values, at the moment I have this; however, it does not work: (directory/* contains file 2 and file 3)

cat file1 | while read line ;do
    for f in $FILES; do

            cat $f | sed 's/11111/$i/g' > newfiles/$f$i.txt



Each time I run the script there may be a different number of files to edit, so I would like to iterate over the directory to make sure all files are covered. There will also be different numbers of strings in file 1.

  • There will be a different number of files in the directory at each time, so i require the script to itterate through all files in the directory. Also, the format of each file may be different so i think sed replace is suitable. – jonny b Jan 20 '19 at 0:14
  • 1
    And why are file1 and file2 the same and file3 and file4 the same? Please edit your question and clarify what you need to do. – terdon Jan 20 '19 at 0:53
  • Apologies, my mistake, i have corrected them. – jonny b Jan 20 '19 at 1:09

You need to iterate through the substitution file and then loop the target files. I think you are almost there. I have something like the following (I subbed some file names so that the globbing would work):


while read r; do
    for f in $FILES; do
        sed "s/11111/$r/g" > newfiles/file$i.txt < $f
        i=$(( i + 1 ))
done <file1

Here we first indicate that we are using bash as the shell. Then set a counter i to 1. Next glob the files into a variable. The loop through file1 and assign each line to variable r. Then for each of the files sub the 11111 string with whatever we saw in the line. Output it to a new file using the counter in the name and then increment the counter. Repeat until the loops are done.

| improve this answer | |
  • Perfect thankyou! – jonny b Jan 20 '19 at 2:39

How about Awk? save the mappings from file1 into an associative array, and then loop over the array for each line of the other files:

awk -v OFS= '
  NR==FNR {t[NR]=$0; next} 
  {s = $0} 
    for(i in t) {
      $0 = s; 
      print > "newfile" ++n
  }' file1 file2 file3

(the OFS= and $1=$1 are just to force the record to be re-evaluated with en empty field separator, so as to remove the spaces between fields). Results:

$ head newfile*
==> newfile1 <==

==> newfile2 <==

==> newfile3 <==

==> newfile4 <==
| improve this answer | |
  • Thankyou! this works great – jonny b Jan 20 '19 at 2:39

I have used below script to achieve the same

for i in `cat mainfile`; do sed "s/11111/$i/g" file1 |sed -r "s/\s+//g" >file1_$i; sed "s/11111/$i/g" file2|sed -r "s/\s+//g" >file2_$i;done


Please find the output of all 4 files



| improve this answer | |

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.