1

I have file 1 which contains:

AAAAAA
BBBBBB

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:

AAAAAA2222233333

File 2:

BBBBBB2222233333

File 3:

22222AAAAAA33333

File 4:

22222BBBBBB33333

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)

FILES=/directory/*
cat file1 | while read line ;do
    for f in $FILES; do

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

    done

done

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
2

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):

!/bin/bash

i=1
FILES=./other_file*
while read r; do
    for f in $FILES; do
        sed "s/11111/$r/g" > newfiles/file$i.txt < $f
        i=$(( i + 1 ))
    done
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
2

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; 
      sub(/11111/,t[i]); 
      $1=$1; 
      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 <==
AAAAAA2222233333

==> newfile2 <==
BBBBBB2222233333

==> newfile3 <==
22222AAAAAA33333

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

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

output

Please find the output of all 4 files

AAAAAA2222233333
============================
BBBBBB2222233333
============================
22222AAAAAA33333
============================
22222BBBBBB33333
==========================

==

| 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.