LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2007, 07:57 AM   #1
Wtr
LQ Newbie
 
Registered: Feb 2005
Posts: 3

Rep: Reputation: 0
script


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?

yes i;m a noob
thanks
 
Old 08-11-2007, 01:36 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.

You know Perl?
 
Old 08-11-2007, 12:56 PM   #3
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
Quote:
Originally Posted by Wtr
i want...
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.
 
Old 08-11-2007, 03:24 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Or use a simple language like gawk which includes a system() function for executing system calls from withing a gawk program.
 
Old 08-11-2007, 06:41 PM   #5
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
Quote:
Originally Posted by PTrenholme
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:

http://steve-parker.org/sh/sh.shtml
 
Old 08-11-2007, 09:11 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
sed -i 's/dir/test/g' dirname?/*.sh
the above is just for n=0-9.
as for changing the filename, its just another statement using mv.
 
Old 08-12-2007, 08:14 AM   #7
Wtr
LQ Newbie
 
Registered: Feb 2005
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks guys. Gonna look into the sed and awk stuff
 
Old 08-12-2007, 01:10 PM   #8
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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!
 
Old 08-13-2007, 02:30 AM   #9
Wtr
LQ Newbie
 
Registered: Feb 2005
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by /bin/bash View Post
Code:
$for i in $(seq 1 20);do mkdir $i;echo "bla bla nr40 bla" >$i/script.sh;done
thanks, but it states "illegal variable name" here.
 
Old 08-13-2007, 05:29 AM   #10
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Code:
$for i in $(seq 1 20);do mkdir $i;echo "bla bla nr40 bla" >$i/script;done
Sorry that was typo!
 
  


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
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 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