Using Shell Script To Loop Program Through Multiple Text Files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using Shell Script To Loop Program Through Multiple Text Files
# 8  
Old 10-27-2011
Quote:
Originally Posted by Corona688
If you can have your fortran program read from standard input and write to standard output, then it will be easily automated. Just redirect into and out of the program.

Your program may become actually simpler; no file-opening code at all Smilie

Code:
for FILE in *.txt
do
        ./myprogram < "$FILE" > "$FILE.out"
done

Sorry I know this is probably a very basic question but I have not been able to grasp this idea really of what you mean by standard input/standard output.

My current open statement reads as
OPEN(unit=10,file='nameofsinglefile.txt',status='old')
Would I adjust the above line to
file='$FILE.txt'

Any help would be greatly appreciated.
Thanks.
# 9  
Old 10-27-2011
I know nothing about Fortran, but by the power of Google:

Fortran Input/Output
Quote:
Preconnected Units

Three unit numbers are automatically associated with specific standard I/O files at the start of program execution. These preconnected units are standard input, standard output, and standard error:
  • Standard input is logical unit 5 (also Fortran 95 unit 100)
  • Standard output is logical unit 6 (also Fortran 95 unit 101)
  • Standard error is logical unit 0 (also Fortran 95 unit 102)
Typically, standard input receives input from the workstation keyboard; standard output and standard error display output on the workstation screen.
In all other cases where a logical unit number but no FILE= name is specified on an OPEN statement, a file is opened with a name of the form fort.n, where n is the logical unit number.
# 10  
Old 10-28-2011
Whenever I try to run this script, I get the error For: command not found and Do: command not found.
# 11  
Old 11-01-2011
What shell are you running it in?
# 12  
Old 11-03-2011
I'm running in Bash shell.

I have gotten past my original problem where it was just saying command not found. However, now I am getting the message that the file does not exist when I run through the shell script, however, if I run the fortran script with just one file it does run...so I know the files do exist. And they are all in the same directory.

My bash script is this
Code:
#!/bin/bash
# Running through Fortran Program used to find anomaly of Rain and Temperature

#FILES="/Users/jduncan/Desktop/rawdata/"

for File in *.txt
do
     ./runthrough.o "$File" 

done

And I simplified the fortran code to illustrate if it was running through the files correctly as
Code:
Program runthrough

IMPLICIT NONE

CHARACTER(31)::text,header,header2
CHARACTER(15)::station
CHARACTER(2)::state
CHARACTER(10)::county
REAL::lat,lon
INTEGER::i


OPEN(10,file='"$File"',status='old')

READ (10,'(a10,a15)') text, station
READ (10,'(a8,a2)') text, state
READ (10,'(a7,a10)') text, county
Do i=1,4
   READ (10,'(a)') text
ENDDO
READ (10,'(a27,f8.5)') text, lat
READ (10,'(a27,f8.5)') text, lon
!Do i=1,3
!   READ (10,'(a)') text
!ENDDO
READ (10,'(a)') text
READ (10,'(a)') header
READ (10,'(a)') header2

print*,header

END Program runthrough

# 13  
Old 11-03-2011
Code:
for File in *.txt

Here you say nothing about WHERE you are and so depending where you are , you might effectively have no *.txt...
# 14  
Old 11-03-2011
Do I need to declare a path even if I am in the same directory?

In that directory when I perform a ls *.txt I have approximately 200 files, and my .f90 code is within the same directory.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop through multiple files in bash script

Hi Everybody, I'm a newbie to shell scripting, and I'd appreciate some help. I have a bunch of .txt files that have some unwanted content. I want to remove lines 1-3 and 1028-1098. #!/bin/bash for '*.txt' in <path to folder> do sed '1,3 d' "$f"; sed '1028,1098 d' "$f"; done I... (2 Replies)
Discussion started by: BabyNuke
2 Replies

2. Shell Programming and Scripting

Read files in shell script code and run a C program on those files

HI, I am trying to implement a simple shell script program that does not make use of ls or find commands as they are quite expensive on very large sets of files. So, I am trying to generate the file list myself. What I am trying to do is this: 1. Generate a file name using shell script, for... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

3. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files: for i in 250.*; do || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies

4. UNIX for Dummies Questions & Answers

Writing a loop to manipulate a script and store it in multiple output files

I have a script where the the 9th line looks like this: $filename=sprintf("250.1chr%d.ped", $N); I want to modify this script 1000 times, changing 250.1chr%d.ped to 250.2chr%d.ped, 250.3chr%.ped.......and so on all the way to 250.1000chr%d.ped and store each output in files called ... (4 Replies)
Discussion started by: evelibertine
4 Replies

5. Shell Programming and Scripting

Removing matching text from multiple files with a shell script

Hello all, I am in need of assistance in creating a script that will remove a specified block of text from multiple .htaccess files. (roughly 1000 files) I am attempting to help with a project to clean up a linux server that has a series of unwanted url rewrites in place, as well as some... (4 Replies)
Discussion started by: boxx
4 Replies

6. Shell Programming and Scripting

Shell script to run a python program on multiple entries in a file

Hello I am trying to run a python program using shell script, which takes a single argument from a file. This file has one entry per line : 1aaa 2bbb 3ccc 4ddd 5eee ... ... ... My shell script runs the program, only for the last entry : #!/bin/sh IFS=$'\n' for line in $(cat... (2 Replies)
Discussion started by: ad23
2 Replies

7. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

8. Shell Programming and Scripting

Storing the values in text file using while loop in shell script

Hi Frdz while read line do name=`echo $line | cut -d' ' -f 1 ` password=`echo $line | cut -d`-` -f 2` name > logfile.txt password > logfile.txt done < list.txt When it is run, am getting last values in list.txt file only,it is not storing lall the list entry values. How can i... (5 Replies)
Discussion started by: KiranKumarKarre
5 Replies

9. Shell Programming and Scripting

problem accessing Multiple Variables from C Program to a Shell script

program name--test #!/bin/bash output1=`/home/user/a.c` output2=`/home/user/a.c` k=`$output1 + 1` m=`$output2 + 1` echo $k echo $m --------------------------------------------------------------------------- prgram name--a.c #include<stdio.h> int main() (1 Reply)
Discussion started by: sameworld1980
1 Replies

10. Shell Programming and Scripting

want to run different files under the same program using shell script

suppose have different files 1.1 2.2 3.3 4.4 5.5 All the files have to run under the same command say tr -d '\n' so how to run all the files under the same command by using shell script (3 Replies)
Discussion started by: cdfd123
3 Replies
Login or Register to Ask a Question