-2

I have 30 files(ascii),which I want convert to binary.Linux command line(FORTRAN 77 CODE) that has been compiled

./rec_binary 

Relevant part of the code

      character*72 ifname,ofname
c
      write(*, fmt="(/'Enter input file name')")
      read(5,85) ifname
85    format(a72)
      write(*, fmt="(/'Enter output file name')")
      read(5,85) ofname

Then code asks for input and output file names

Enter input file name
rec01.txt

Enter output file name
rec.01

How to automate this?I have tried like this

#!/bin/csh -f
set list = 'ls rec*.txt'
foreach file ($list)
rec_binary ${file} > 

Or

#!/bin/sh
for f in .txt
do
./rec_binary F
done

But I do not have a clue for next step.Text files are

rec01.txt
rec02.txt

rec30.txt

Output files

rec.01
rec.02

rec.30
Richard Rublev
  • 7,718
  • 16
  • 77
  • 121

3 Answers3

2

Try this:

#!/bin/bash
for each in `ls rec*.txt`
do
  op_file=$(echo $each | sed 's/\(rec\)\([0-9]*\).txt/\1\.\2/')
  ./rec_binary <<EOF
$each
$op_file
EOF
done

The variable op_file converts your rec01.txt to rec.01.

Fazlin
  • 2,285
  • 17
  • 29
  • No.I got this: ad.sh: command substitution: line 3: unexpected EOF while looking for matching `"' ad.sh: command substitution: line 4: syntax error: unexpected end of file – Richard Rublev Jun 17 '16 at 14:24
  • Be careful with the line indentation. Try to follow the indentation as i have used in the script. – Fazlin Jun 17 '16 at 14:30
  • Sorry my bad. I had a extra `"` in the script. Please try the latest edited version – Fazlin Jun 17 '16 at 14:31
1

Assuming , you have knowledge of rec_binary. I am not sure what it do. I am making this up based on your inputs in question.

for i in rec*.txt;
 do
    rec_binary "$i"
done
P....
  • 17,421
  • 2
  • 32
  • 52
0

There are a bunch of different ways you could do this. One way would to be using a for loop, however, it's unclear if the rec_binary command you've shown allows arguments.

for i in rec*.txt; do
    num=$( echo $i | egrep -o "\d+" )
    echo ${i} > "rec.${num}"
done 

If you can do something like this from the command-line ./rec_binary file1 file2, then that should work. If the rec_binary command echos to stdout then you could send it to a file:

for i in rec*.txt; do
    num=$( echo $i | egrep -o "\d+" )
    rec_binary ${i} > "rec.${num}"
done

The $num variable simply captures the digits from the filename as it loops and then we can use it while running the next command to construct the filename.

l'L'l
  • 44,951
  • 10
  • 95
  • 146