LinuxQuestions.org
Help answer threads with 0 replies.
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 05-31-2006, 05:10 PM   #1
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 141

Rep: Reputation: 15
Help on a command for a script.


I have a problem that might be solved with an easy script:
i have a text file, which is processed by a program what gives me two output files.
I have the need to automate the following task:
by a script i need to change the value of a line of the file
process with the program
then move the 2 output files to a directory
change again the value
process
and so on..
until all the values are processed..

What is the command that would allow me to change the value line in sequence each time sending the process command to the main prog?

i had something like that
#!/bin/sh
mkdir ./done
for I in `ls *.w`
do
echo "start $I"
./mcnp inp=$I xsdir=xsdir2
mkdir ./done/$I
mv $I ./done/$I
mv outp ./done/$I > /dev/null
mv mctal ./done/$I > /dev/null
rm runtpe > /dev/null
echo "done $I"
done

which processed in sequence different files named with *.w..
however this time is different, i need to change the value of a line in the file, and process it..
the modified line can be either stored into the script file (i need to store only like 15 lines) or on a separate file..

any idea about how to do it?
Thanks
 
Old 06-01-2006, 10:58 AM   #2
jyoung4
LQ Newbie
 
Registered: Apr 2006
Location: Minneapolis, Minnesota, USA
Posts: 16

Rep: Reputation: 1
The sed command may be able to do what you want. You can use it to search the file and replace current value with the new value. The sed command for your script might look something like this:

Code:
sed -e"s/$oldValue/$newValue/" $I > /tmp/$I
In this example sed looks at each line of the input file $I for $oldValue and replaces it with $newValue. Output from sed is to stdout so you need to redirect it somewhere. If your ./mcnp program can take input from stdin, you could pipe directly from sed to it.
 
Old 06-01-2006, 12:49 PM   #3
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 141

Original Poster
Rep: Reputation: 15
thanks alot..there is another small issue, unfortunately..
in the file there is another entry that needs to be edited at the same time:
E18 value-0.02 value
where value is the $newValue..how can i make it perform operations as well?
 
Old 06-02-2006, 02:43 AM   #4
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
Post (some of) the file that is input and post your intented output and we can show you how to use sed.
This might help also: http://bookmarks.linuxquestions.org/...tries/tags/sed
 
Old 06-02-2006, 05:16 AM   #5
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 141

Original Poster
Rep: Reputation: 15
Question

this is the part of the file that needs to be modified:

SDEF POS=0 0 7.631 par=2 ERG=0.88
m1 013000.01p 1 COND=1
m2 029000.01p 1 COND=1
m3 032000.01p 1 COND=1
m4 007000.01p -0.8 GAS=1
008000.01p -0.2 GAS=1
F18:P 2
FC18
E18 0 0.86 0.89

i need the number after ERG to be modified each time the file is processed, at the same time the 2 valued of E18 should be modified as
J-0.02 J+0.01 where J is the modified value in ERG.
is absolutely necessary that the spaces in E18 are preserved since if the number of spaces is changed the program will refuse to process the file.

Actually what i mainly need is this:
start the script, the script put the first value and process the file, once the program ends, the script put the second value and process..
until all the values are processed..
since the program won't overwrite the output files but rather rename them is not a big deal if the script doesn't move to another location, and also will make the script easier..
 
Old 06-02-2006, 05:34 AM   #6
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
I'm not behind my computer at the moment (sadly )
so i can't test if my examples work.
Take a look at this example on how to use variables and use the correct variables in some sort of loop:
Code:
cat test.txt | sed -e "s/foo/$SOMEVARIABLE/g"
sed 's/'`echo ${SOMEVARIABLE}`'//g' test.txt
Replace foo with the contents of a variable.
source

What you want is something like this (backup the inputfile first and don't use -i while testing to display the output to stdout):
Code:
sed -i "s/\(^SDEF\ POS=0\ 0\ 7.631\ par=2\ ERG=\)(.*)/\1$J/g" $I
To do the adding, try:
echo $((J-0.02))
(i don't know if decimals work here ...)
/EDIT: they don't, so i don't know how to work decimals like this.
Do the adding before substituting with sed. /EDIT

To combine the two sed's try:
Code:
sed -i "
s/\(^SDEF\ POS=0\ 0\ 7.631\ par=2\ ERG=\)(.*)/\1$J/
s/\(^E18 0\ \)(.*)/\1`echo $((J-0.02))`\ `echo $((J+0.01))`/
" $I
It might be easier to do the addition before doing the sed, so you can use variables like we use $J.

Last edited by muha; 06-06-2006 at 07:26 AM.
 
Old 06-05-2006, 08:36 AM   #7
jyoung4
LQ Newbie
 
Registered: Apr 2006
Location: Minneapolis, Minnesota, USA
Posts: 16

Rep: Reputation: 1
Yes - as muha showed in the previous post, you can do multiple substitutions in a single pass over the input. You can even put your sed "program" in a separate file and use the -f option on the call line to go there to find all the commands.

With sed, as with UNIX/Linux in general, there are often times many ways to do the same thing. In your case you might be able to use line ranges on your sed commands to simplify them a bit. I would suggest you might find studying the sed man page useful. It's a half decent one and will clarify what we're pointing you towards.
 
  


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
script command trasher Linux - Newbie 5 02-06-2006 06:20 AM
System command script RHrulz Mandriva 7 03-26-2005 11:33 AM
sqlplus command from script Grassie Coetzee LinuxQuestions.org Member Intro 2 03-13-2005 12:01 PM
Command in shell script Grassie Coetzee Linux - Software 1 03-13-2005 11:42 AM
script 'exit' command kilobravo Linux - General 8 01-16-2003 03:24 PM

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

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