LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to change the content of a file without open it? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-change-the-content-of-a-file-without-open-it-712929/)

keupie 03-19-2009 03:38 PM

how to change the content of a file without open it?
 
I am using condor system to run simulation. Each run I only need to change a number in the input file. For example, for 20 simulations, I have 20 input files. But only a number in the input file is different. How can I change the number without opening each file and changing it manually?

thanks so much.

Mark_667 03-19-2009 03:50 PM

If I understand you right then Condor (I presume you're talking about http://www.cs.wisc.edu/condor/) needs to read a number from a file to tell it how many input files it should use per run?

The only way you could do this would be to know how many input files it'll need per run in advance or have some way of calculating it. If you do you could probably just use a script to write a file whenever it detects that a previous job has completed (maybe by going off the number of files output or the contents of a log file?). You may want to set this to run automatically in a cron job.

keupie 03-19-2009 04:31 PM

Quote:

Originally Posted by Mark_667 (Post 3481217)
If I understand you right then Condor (I presume you're talking about http://www.cs.wisc.edu/condor/) needs to read a number from a file to tell it how many input files it should use per run?

The only way you could do this would be to know how many input files it'll need per run in advance or have some way of calculating it. If you do you could probably just use a script to write a file whenever it detects that a previous job has completed (maybe by going off the number of files output or the contents of a log file?). You may want to set this to run automatically in a cron job.

Yeah. I am using that condor. This can run the 20 jobs at the same time, but at different machines. I have a folder to store the input file for each run. This is the same as I run a program 20 times with different inputs. I want to know in linux, do we have a way to change 20 files with the same name ,in different folders easily? I only need to change the number in the third line to a different number for each file.

thanks

keupie 03-19-2009 04:58 PM

Quote:

Originally Posted by keupie (Post 3481256)
Yeah. I am using that condor. This can run the 20 jobs at the same time, but at different machines. I have a folder to store the input file for each run. This is the same as I run a program 20 times with different inputs. I want to know in linux, do we have a way to change 20 files with the same name ,in different folders easily? I only need to change the number in the third line to a different number for each file.

thanks

I found this command
sed -i 's/-15/-2035/' filename

this works.

syg00 03-19-2009 06:10 PM

That may lead to some astonishment - it changes all occurrences of "-15" TO "-2035". Maybe you should just change the data you need to the data you want. Note the 3 after the single quote restricts the update to only the third line.
Code:

sed -i '3s/-15/-20/' filename
There are also simple script solutions to iterate over a director structure to do this automatically in (applicable) all files, but requires knowledge of how you have said directories organized.

JaksoDebr 03-19-2009 06:20 PM

Running echo in a shell with output redirection can overwrite file contents. For example:
echo "12" > some-file.txt
will overwrite any existing some-file.txt with 12 as it's content.

By creating a shell script you can populate all the input files with random numbers. Adding the shell script as a cron job will run it without your interaction.

Linux Archive


All times are GMT -5. The time now is 10:59 AM.