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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Detail on For loop for multiple file input and bash variable usage
# 1  
Old 03-16-2013
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

Code:
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

Code:
for loop

awk script file_1 file_2 file_3

done

in next increment it should
Code:
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.
# 2  
Old 03-16-2013
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:
Code:
#!/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:
Code:
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:
Code:
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

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-16-2013
Thank you so much
My .for code looks like this...
Code:
    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
Code:
$file  for input

Code:
$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
# 4  
Old 03-16-2013
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
# 5  
Old 03-16-2013
Hi.

See also related intrinsic for f77:
Quote:
]8.98 GETARG - Get command line arguments

Description:
Retrieve the POS-th argument that was passed on the command line when the containing program was invoked.

This intrinsic routine is provided for backwards compatibility with GNU Fortran 77. In new code, programmers should consider the use of the GET_COMMAND_ARGUMENT intrinsic defined by the Fortran 2003 standard.
...
excerpt from GETARG - The GNU Fortran Compiler

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash Variable scope - while loop while reading from a file

Cope sample1: test.sh i=0 echo " Outside loop i = $i " while do i=$(( $i + 1)) echo "Inside loop i = $i " done echo " Out of loop i is : $i " When run output : Outside loop i = 0 Inside loop i = 1 Inside loop i = 2 Inside loop i = 3 Inside loop i = 4 Inside loop i = 5 Inside... (8 Replies)
Discussion started by: Adarshreddy01
8 Replies

2. Shell Programming and Scripting

While loop with input in a bash script

I have the following while loop that I put in a script, demo.sh: while read rna; do aawork=$(echo "${rna}" | sed -n -e 's/\(...\)\1 /gp' | sed -f rna.sed) echo "$aawork" | sed 's/ //g' echo "$aawork" | tr ' ' '\012' | sort | sed '/^$/d' | uniq -c | sed 's/*\(*\) \(.*\)/\2: \... (3 Replies)
Discussion started by: faizlo
3 Replies

3. Shell Programming and Scripting

Bash loop hording keypress input

I have a bash loop that waits for a single key press, then does $something depending on what $key is pressed before refreshing the screen with updated data. The problem I have is that the script will store additional key presses and chain them together causing the screen to redraw and the script... (1 Reply)
Discussion started by: DarkPhoenix
1 Replies

4. AIX

Disk Usage in Detail !!

Hi ALL, I always use du command for analyzing diskusage to housekeep a filesystem. Can somebody tell the where first row filesize resides? bash-3.2# df -gt /tmp Filesystem GB blocks Used Free %Used Mounted on /dev/hd3 16.50 10.01 6.49 61% /tmp bash-3.2# bash-3.2# pwd... (6 Replies)
Discussion started by: Thala
6 Replies

5. Shell Programming and Scripting

FOR loop with multiple files as input and awk

Hi all , i want to pass multiple files as input to a for loop for i in file1 file2 file3 do some awk action < $i >> $i.out done but im getting error in that for loop is the way i use to pass files to awk using for correct and 2.we can directly pass multiple files to awk as... (7 Replies)
Discussion started by: zozoo
7 Replies

6. Shell Programming and Scripting

(BASH) Using a loop variable to grep something in a file?

Hi, I have a loop running until a variable L that is read previously in the full script. I'd like to grep some information in an input file at a line that contains the value of the loop parameter $i. I've tried to use grep, but the problem is nothing is written in the FILE files. It seems grep... (5 Replies)
Discussion started by: DMini
5 Replies

7. UNIX for Dummies Questions & Answers

Writing a for loop that processes multiple input files

I would like to write a for loop that does the following: I have a file called X.txt and other files called 1.txt,2.txt, .....,1000.txt. I want to substitute the 6th column of the file X.txt with 1.txt and store the output as X.1. Then I want to do the same with X.txt and 2.txt and store the... (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

9. UNIX for Dummies Questions & Answers

usage of same variable in multiple scripts

Hi, I have a .test file which has: #!/bin/ksh export TEST_FLAG=1 In the test1.ksh i have: #!/bin/ksh . .test echo $TEST_FLAG When i execute the test1.ksh its showing the value as 1. But if i refer the same variable in another script, the value is not 1. Basically, I need to have... (1 Reply)
Discussion started by: risshanth
1 Replies

10. Shell Programming and Scripting

Basic bash 'for loop' usage

Hi! I have a simple question about using a for loop. I'm trying to open up all the zip files in the currect directory with ark, but I am getting the error "bash: syntax error near unexpected token `for $i ; do ark $i ; done ; I looked in the info pages for bash, but I can't seem to figure... (2 Replies)
Discussion started by: Orange Stripes
2 Replies
Login or Register to Ask a Question