LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-09-2011, 11:29 AM   #1
sasanthi
LQ Newbie
 
Registered: Jul 2011
Posts: 11

Rep: Reputation: Disabled
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
 
Old 07-09-2011, 11:49 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 07-09-2011, 11:50 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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,
 
Old 07-09-2011, 12:49 PM   #4
sasanthi
LQ Newbie
 
Registered: Jul 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
that works! many thanks for your help!
 
Old 07-10-2011, 03:12 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Brilliant Could you mark the thread as 'SOLVED', then? Thanks,
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Command Substitution in a Bash Shell Script LXer Syndicated Linux News 0 02-03-2011 07:30 PM
[SOLVED] shell script -- awk variable substitution r2d2#jedi Linux - General 2 10-07-2010 09:04 PM
exact substitution is not made. in shell script amit_pansuria Programming 11 11-22-2008 12:19 PM
Varible substitution in Bash script RonV Linux - Newbie 2 06-04-2006 12:11 PM
shell question variable substitution dpressm Programming 9 11-18-2004 07:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration