On 2017-06-16 01:18, Ken Rock wrote:
> I hope I'm on the correct user group.
Assuming that you want an answer that will work under bash, you're in
the right news group.
> I need to input a text file into a program and store the treated text file. Using mac Terminal, I can get it to work cmd/program <text001.txt >nutext001.txt. I have to do this many times i.e. text001.txt to text100.txt.
> This will be very tedious in Terminal by direct input. Please can anyone suggest a quicker way to do it?
How about something along the lines of:
for file in text???.txt
do
cmd/program < $file > nu$file
done
If the names of the files that you need to proess don't actually
match "text???.txt", or if you have other files that match it,
you could change the regular expression, or you could move just
the relevant files to a subdirectory created just for this task.
However, if you use a different directory, you'll need to change
"cmd/program" to correctly match the relative path to program.
Another option in place of "for file in text???.txt" that you might
use if you only want to deal with the first 100 files:
n=1001
while [[ $n -le 1100 ]]
do
fn=text`echo $n | sed 's/^1//'`.txt
cmd/program < $fn > nu$fn
((n+=1))
done
What I've done here is come up with a way to count from 001 through
100, with the output always having three digits, demonstrated here:
username@hostname$ n=1001
username@hostname$ echo $n | sed 's/^1//'
001
username@hostname$ ((n+=1))
username@hostname$ echo $n | sed 's/^1//'
002
username@hostname$
The variable "fn" then gets this counter, preceded by the string "text"
and followed by the string ".txt".
The sed command just strips off the leading "1" on the fly. Why do this?
Because if you just initialize to "001" and then increment, this'll
happen:
username@hostname$ n=001
username@hostname$ echo $n
001
username@hostname$ ((n+=1))
username@hostname$ echo $n
2
username@hostname$
P.S. Make sure to create a scratch directory and copy all of the
contents of your base directory there, in case of errors.
--
Michael F. Stemper
Nostalgia just ain't what it used to be.