LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script for substitution (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-substitution-890796/)

sasanthi 07-09-2011 11:29 AM

shell script for substitution
 
Dear all,

I am struggling with a substitution in multiple files by using shell scripting and I would appreciate if anyone could help.
So, I have 100 folders each of them include a file called script.txt and script2.txt. the file script.txt has and 100include files that have the same name like the folders. So, for example if I have the folders
A, B and C
then the script.txt will include 3lines such as:

*include A
*include B
*include C

I want for each folder (A, B and C) to create script2.txt which will substitute *include A with $include A, respectively for each folder.

so for folder A, I will have script2.txt
$include A
*include B
*include C

for B:
*include A
$include B
*include C

I tried the followed script:
#!/bin/bash
for name in *
do
find . -iname '*_' |sed 's/^..//' | while read line;
do
if [ "$line" != "$name" ]
then
perl -pe 's/\*include $line/\$include $line/' $name/HyperDM.gms > $name/HyperDM_scen0.gms
fi
done
done


but it is substituting "*" with "$" in each file for all the lines. could someone help me? I think i should probably close earlier a loop, but I am totally confused and cannot solve it.

thanks, chrysanthi

grail 07-09-2011 11:49 AM

Well i am guessing that script.txt is in the top folder as well? ie you have folders A, B and C along with script.txt

If the above assumption is correct (and I understand the question correctly):
Code:

for dir in *
do
    [[ -d "$dir" ]] && sed "/$dir$/s/^./$/" script.txt > "$dir/script2.txt"
done


Snark1994 07-09-2011 11:50 AM

This works for me:

Code:

for name in *; do
    sed 's/\*include '${name}'/\$include '${name}'/' ${name}/script1.txt >${name}/script2.txt
done

Hope this helps,

sasanthi 07-09-2011 12:49 PM

that works! many thanks for your help!

Snark1994 07-10-2011 03:12 AM

Brilliant :) Could you mark the thread as 'SOLVED', then? Thanks,


All times are GMT -5. The time now is 05:15 AM.