Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script

for file in `ls *.txt |sort -t"_" -k2n,2`; do

awk script  $file

done

which sorts file in order, and will input one after another file in order to awk script

suppose if I have to input 2 or more than two file in order to awk what I need to do

case is something like this

for loop

awk script file_1 file_2 file_3

done

in next increment it should

input file_4 file_5 and file_6

and I do program normally by FORTRAN or C, don't know much about shell

every time I have to edit in and out file in Fortran code and then I have to compile and run .for code

is there any solution like EOF for fortran to use bash variable in fortran code for in and out file replacement and execute the same.

Kindly anyone explain on this topic.

I don't understand what you're asking about fortran code, but getting sets of 3 files in sorted order in the shell is not hard. This doesn't do anything very useful, but may be a suitable example to show you what needs to be done to meet your requirements:

#!/bin/ksh
ls *.txt|sort -t"_" -k2n,2|while read f1 && read f2 && read f3
do      echo processing $f1, $f2, and $f3
done

Note that this won't work if any of your *.txt filenames contain whitespace characters, and will silently ignore the last one or two *.txt files if the number of *.txt files is not an integral multiple of 3. In a directory containing the files:

ABC_6.txt
B.txt
DEF_5.txt
GHI_7.txt
a.txt
abc_123.txt
bcd_987.txt
cde_456.txt
tester
z.txt

this script produces the output:

processing B.txt, a.txt, and z.txt
processing DEF_5.txt, ABC_6.txt, and GHI_7.txt
processing abc_123.txt, cde_456.txt, and bcd_987.txt
1 Like

Thank you so much
My .for code looks like this...

    character*70 dat,dat1,dat2,inpfile,outfile
    integer ctd,s,b(15000)
    real var1(15000),var2(15000),var3(15000),below
    real var5,var6,e,e1,a

    read(*,*)inpfile
    open(10,file=inpfile,status='old')

    read(*,*)outfile
        open(11,file=outfile,status='unknown')

for each text file here I have to replace inp and outfile every time, if many files means its time consuming

if it was shell I would have done

$file  for input 
$file>"out"$file for output 

So I am just wondering some provision to use bash variables in place of inpfile and outfile in .for code

anyways thank you so much for your valuable reply

I assume that you want your Fortran code to be able to look at its command line arguments rather than recompiling your source with different values for input and output filenames derived from shell variables at compile time.

If your Fortran compiler complies with the 2003 Fortran standard, try accessing the comand line arguments as specified here: GET_COMMAND_ARGUMENT

Hi.

See also related intrinsic for f77:

excerpt from GETARG - The GNU Fortran Compiler

Best wishes ... cheers, drl