Jan Rostenkowski
Jan Rostenkowski

Reputation: 325

Copying contents of sets of text files into other sets of text files

I am in a directory containing a set of folders A1-7, and B1-7, such that if I give the "ls" command, I will see the following.

A1 A2 A3 A4 A5 A6 A7 B1 B2 B3 B4 B5 B6 B7 

The B folders contain a single text file, while the A folders contain an input file and a submission script. The files are named for their containing folders, e.g., the A1 folder contains A1.inp and A1.sub, and the B1 folder contains B1.txt.

The goal is to have the contents of the B1 text file be copied into a certain position in the A1 input file, specifically below the string of text "xyz". Similarly, The B2 text file should be copied into the A2 input file of the "xyz" string.

Is there a single command/script that can add all the contents of the B text files into the corresponding A input files?

Upvotes: 0

Views: 42

Answers (1)

B. Shefter
B. Shefter

Reputation: 897

Something like this should work:

for i in {1..7}; do sed -i '/xyz/r B${i}/B${i}.txt' A${i}/A${i}.inp; done

The -i flag for sed means to edit the file (A#.inp) in place. The part in single quotes is the command you want to execute on that file; r <filename> appends the text read from filename (B#.txt).

Upvotes: 1

Related Questions