Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a shell command and use the result in a Fortran program?

Tags:

shell

fortran

Is it possible to call shell command from a Fortran script?

My problem is that I analyze really big files. These files have a lot of lines, e.g. 84084002 or similar. I need to know how many lines the file has, before I start the analysis, therefore I usually used shell command: wc -l "filename", and than used this number as a parameter of one variable in my script.

But I would like to call this command from my program and use the number of lines and store it into the variable value.

like image 565
Pavol Namer Avatar asked Jan 09 '23 13:01

Pavol Namer


1 Answers

Since 1984, actually in the 2008 standard but already implemented by most of the commonly-encountered Fortran compilers including gfortran, there is a standard intrinsic subroutine execute_command_line which does, approximately, what the widely-implemented but non-standard subroutine system does. As @MarkSetchell has (almost) written, you could try

CALL execute_command_line('wc -l < file.txt > wc.txt' ) 
OPEN(unit=nn,file='wc.txt') 
READ(nn,*) count 

What Fortran doesn't have is a standard way in which to get the number of lines in a file without recourse to the kind of operating-system-dependent workaround above. Other, that is, than opening the file, counting the number of lines, and then rewinding to the start of the file to commence reading.

like image 183
High Performance Mark Avatar answered Apr 26 '23 23:04

High Performance Mark