Doing a loop to change a value in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doing a loop to change a value in a file
# 1  
Old 12-06-2012
Doing a loop to change a value in a file

Hi, I have an N number of files in a directory. I like to write a shell script that would make identical plots for each one of these files.

The files have names such as:
Code:
t00001.dat
t00002.dat  
t00003.dat  
t00004.dat  
t00005.dat
       .
       .
       .
t00040.dat

i.e. the filename is incrementing numerically from a value of 1 to N.

I plot some variables from these files using gnuplot. So in my gnuplot plotting script (plot.gplt) I have the following line which plots the function:

Code:
plot 't00001.dat' using 1:2

I know that using a perl script I could change the filename 't00001.dat' incrementally. But with my limited knowledge in shell scripting I don't know how to do this.

Thanks!
# 2  
Old 12-06-2012
How about:-
Code:
printf "%s\n" t*.dat | sort | while read file
do
   plot '"$file"' using 1:2
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-06-2012
Code:
for file in t*.dat
do
  plot "'$file'" ...
done

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-06-2012
Code:
for i in {1..40}; 
do 
 str=$(printf %05d $i)
 plot \'"t$str.dat"\' using 1:2
done

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 12-07-2012
Thanks for the replies. I'm sorry I didn't explain my problem clearly.

I have a gnuplot script file that does the plotting (called time.gplt), in it I have the following line that plots the data.

Code:
plot 't00001.dat' using 1:2

Using rdcwayx's method I've been able to do most of what I want, i.e:

Code:
for i in {1..40};
do
        str=$(printf %05d $i)
        perl -i -pe "/plot/&&s/t\d+/t$str/" time.gplt
        gnuplot time.gplt
done

But now I have the following questions

1) is it possible to use something like
Code:
n=$(ls -l t?????.dat | wc -l)

to calculate the number of files and then use that to define the number of loops?

2) Also I have the following line in my gnuplot script (time.gplt) which I would like to modify:

Code:
set label at 14,10 '$t=1.0$~ms'

I would like to replace the number after the equal sign in the above code with the variable $str?

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Change argument in a for loop

In a "for i in *FD.CPY do" loop, I need to change .CPY to .layout so the executed command would be reclay blahFD.CPY >blahFD.layout What do I need to do to modify a copy of i to use after the > symbol? TIA (5 Replies)
Discussion started by: wbport
5 Replies

2. UNIX Desktop Questions & Answers

Change name of files to their paths -- find loop

Dear All, I have many sub-folders but each of them have a file with same name but different data. I want to either move or copy them into a new folder but they need to have the path of where they are coming as part of their name... I have managed to find the files but dont know how to change... (2 Replies)
Discussion started by: A-V
2 Replies

3. Shell Programming and Scripting

Change a field value in a loop

Hi guys, i have an executable file that contains several records and fields. One of the records has a variable filed that must be changed each time i want to execute the file. Would it be possible that i can use a loop to change the value of that field? Suppose that the field address is: Record... (5 Replies)
Discussion started by: saeed.soltani
5 Replies

4. Shell Programming and Scripting

Running ffmpeg in loop seems to change reading from file

Hi everyone, I am fairly new to shell scripting. I want to read in numbers from a file (one number per line). This works perfectly fine while read CurrentLine do echo $CurrentLine done < myfile and yields the correct output: 272 745 123 If I however run a ffmpeg... (2 Replies)
Discussion started by: Thriceguy
2 Replies

5. Shell Programming and Scripting

Variable value dosent change after the loop changes it

i am new to shell scripting. this is similar to the code which i was writing for some other work. i want the variable 'x' to have the value which it will finally have at the end of the loop ( the number of directories ). but the value of 'x' only changes inside the loop and it remains '0' out-side... (3 Replies)
Discussion started by: chathura666
3 Replies

6. Shell Programming and Scripting

Variable name change in a loop

I need to do something like this:for I in var1 var2 var3 ; do $I = "Something calculated inside the loop" doneObviously it doesn't work...but is it possible doing that in other ways? Thanks in advance. (6 Replies)
Discussion started by: canduc17
6 Replies

7. UNIX for Dummies Questions & Answers

change if-else loop to while-do

Hello friends, i wrote a script which includes a couple of if-else loops but i have to change it to while-do loop as it is not allowed to use "break" in if-else loop in bash. #!/bin/bash -x NUM=`find . -name ufsdump_output1.txt | xargs egrep "End-of-tape detected"` if ; then echo "OK!"... (6 Replies)
Discussion started by: EAGL€
6 Replies

8. Shell Programming and Scripting

loop through file to change some data

Hi, I have a file with the following data -0.00000 0.00000 0.00000 F F F 0.00000 0.00000 0.80000 F F F 0.50000 0.50000 0.60000 F F F 0.50000 0.50000 0.20000 F F F -0.00000 0.00000 0.40000 F F F I would like to change the last 3 lines from F F F to T T T. I tried looping each line but don't... (5 Replies)
Discussion started by: princessotes
5 Replies

9. Shell Programming and Scripting

'while' loop does not change local variables?!

(I think this question desearves separate thread..) I have a problem with 'while' I am trying to set variables by 'while' and it is fine inside, but after completting the loop all changes are lost: > bb="kkkk - 111\nlllll - 22222\nbbbb - 4444" > echo "$bb" kkkk - 111 lllll - 22222 bbbb -... (3 Replies)
Discussion started by: alex_5161
3 Replies
Login or Register to Ask a Question