ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello;
i want to write a script that:
opens a directory
(opens another script and) changes 1 value somewhere in the text,
and saves again this file under a different name.
as in:
cd "dirname"n with n running from 1 to 100 for example
vi filename
:.,$s/nr40/job-n-
to change text blabla nr40 to blabla nr-n-
or use sed or something?
and then save as filename-n-
so how do u do the loop over n and how to do this correctly?
I haven't replied up to now, hoping someone else would come along.
I was hoping someone would come along with a cool, simple, elegant solution. Maybe that will still happen.
My approach would be to use Perl. That gives you as much control over the situation as you could want. "Control" means things like checking the input data for validity if you wish.
Although Perl is a flexible language & an excellent suggestion, its learning curve cannot be ignored.
I am guessing that the original poster wants to write a shell script to automate this task. If so, your best option (outside of using a dynamic language like Perl or Python...) is to learn sed. You can find a number of sed tutorials on the Web (simply search for sed tutorial...). Wikipedia has an article on sed & IBM developerWorks has a series of articles which cover learning sed.
Or use a simple language like gawk which includes a system() function for executing system calls...
awk(gawk == GNU awk) is yet another good choice. I just remembered the following shell scripting tutorial which might be good material for gluing awk code or sed code together:
I made 20 directories. I put a file in each directory script. In each script is the text "bla bla nr40 bla"
Now you want to go into each directory and change script so nr40 is nr<dirno> and also rename the file to script<dirno>.
Code:
$for i in $(seq 1 20);do mkdir $i;echo "bla bla nr40 bla" >$i/script;done
$cat 1/script
bla bla nr40 bla
$for i in $(seq 1 20);do sed -i "s/nr40/nr${i}/" $i/script;mv $i/script $i/script$i;done
$cat 1/script1
bla bla nr1 bla
HTH
Last edited by /bin/bash; 08-13-2007 at 05:30 AM.
Reason: Correct typo!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.