George
George

Reputation: 189

Error in SED Directory Creation

I'm trying to use SED to create a series of directories that all contain a file of the same name, yet each file in each directory varies by one line.

for i in $( cat ~/SCRIPTS/AALIST.txt);  do

mkdir ~/jan10/25

sed -e "s/HAT/${i}/"  <~/SCRIPTS/HAT25.inp > ~/jan10/25/25$i/mat.inp

AALIST.txt is simply a file containing, effectively..

A
B
C
D
E

Whereas HAT25.inp has many lines of text, and then the word "HAT" on the 25th line, this HAT should be replaced with A,B,C, etc. In directory 25, SED should be creating directories called 25A, 25B, 25C, etc. Inside of these directories should be the original HAT25.inp file with HAT replaced with the letter of the parent directory. By this, I mean that the directory 25A should contain HAT.inp renamed mat.inp and containing A at the 25th line, rather than HAT. Unfortunately, when I try to execute my code above, I get the error:

cannot create directory `/home/user/jan10/25': File exists
./loop.sh: line 23: /home/user/jan10/25/25$i/mat.inp: No such file or directory

Is there any insight on what I've done wrong, I'm having trouble determining exactly what error I've made.

EDIT:

By commenting out the mkdir line, I now receive the following error. The directory "25" is created, but directories 25A, 25B, etc. are not created. The error received is below.

./loop.sh: line 23: /home/user/jan10/25/25A/mat.inp: No such file or directory

Upvotes: 1

Views: 79

Answers (1)

NeronLeVelu
NeronLeVelu

Reputation: 10039

for i in $( cat ~/SCRIPTS/AALIST.txt )
 do

   NewFolder=~/jan10/25/25${i}
   mkdir "${NewFolder}"

   sed -e "s/HAT/${i}/" ~/SCRIPTS/HAT25.inp > "${NewFolder}/mat.inp"

done
  • Your code was missing some info (end of loop, folder end name at creation and mayb some space in $()
  • some enhancemnet (sed don't need a < and can directly take the file as parameter (avoid a subshell), use of surrounding {} for variable

Upvotes: 1

Related Questions