Looping through files...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping through files...
# 1  
Old 11-07-2007
Looping through files...

I posted this in the Solaris forum, but I don't think it's platform specific, so I'm posting it here.

Here is the situation. We are a company that has been using a professional publishing system, the software is called "ProType". It runs on Solaris 2.4, however it is no longer supported and we are forced to move on to Adobe Indesign. We must convert all our documents (thousands) to InDesign format. ProType comes with an export utility that exports the text to ASCII with all the formatting etc.

The problem is that the export process takes some time for each file. It is taking a VERY long time to export these documents.

How difficult would it be to loop through the documents in a folder and export them?

This is the exact process if I were doing it manually:

1) Type: cd /bedford6:/export

2) Then I would type the directory of where the files I want to export are, for example cd /home/mesorah/layout

3) Then I would type: expage P Pg001 > Pg001 where "P Pg001" would be the name of the current file and "Pg001" would be then name of the exported file (the exported file needs to drop the first P).

Could this be automated with a script to loop through every document in a directory while making sure to leave out the P when typing the exported page?

Last edited by Fred Goldman; 11-07-2007 at 05:59 PM..
# 2  
Old 11-07-2007
I don't see the point of running the "cd" command twice in a row, unless the "expage" program is using $OLDPWD.

Since you say there are thousands of documents, the loop "for i in *" trick is probably a bad idea. Ditto on the spaces in the filenames (I assume "P Pg001" is a filename?). So:

Code:
ls | while read FILE; do
  NEWFILE=${LINE##* }
  echo $(date +%H:%M) expage "$FILE" \> "$NEWFILE"
  expage "$FILE" > "$NEWFILE"
done

The form "${LINE##* }" should remove everything up to and including the last space from the left. It would seem to be necessary to quote "$LINE" when running expage, if the filename really has embedded spaces.

If it isn't obvious: You should test this in a directory with a few files that are backed up somewhere until you're comfortable that this works the way you think it does.
# 3  
Old 11-07-2007
OK, I am going to try this tomorrow...

The P before the file name is the way ProType recognizes its files like a file extension of some sort (I apologize I know very little about UNIX). I need the export utility to keep the same file name except for the P.

I don't quite understand what you mean by removing the spaces. Do you mean you are going to first rename the files without the spaces?

The way these files are set up is there are about 10-30 files in each directory with about 100 or so directories. I planned on running the script on each directory individually.

Ok (if I haven't until now I am really going to show off my ignorance) how do I call this script.
# 4  
Old 11-08-2007
OK, this is the exact script I am running, I just get a "command not found". Any ideas?

Code:
#!/bin/tcsh
ls | while read FILE; do
  NEWFILE=${LINE##* }
  echo $(date +%H:%M) expage "$FILE" \> "$NEWFILE"
  expage "$FILE" > "$NEWFILE"
done

# 5  
Old 11-08-2007
Hmmm, "tcsh"? Eeeek. I'm not really familiar with that shell. Try:

#!/bin/ksh
#!/bin/zsh
#!/bin/bash
#!/bin/sh

Not necessarily in that order. Smilie One of those should work, though, and I'm pretty sure the syntax I wrote above is all wrong for tcsh.
# 6  
Old 11-08-2007
OK, I think we are getting somewhere.

With ksh I got syntax error line 7 done unexpected
With sh I got syntax error line 4 ( unexpected.
# 7  
Old 11-08-2007
OK, Ive done a little research and this should be the syntax for tcsh:

Code:
#!/usr/bin/env tcsh

foreach file ($*)
  set newname="$file.txt"
  export "$file" /> "$newname"
end

The only thing I can't figure out is how to drop the p and the space.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping through files in pairs

Hi all, Please guide. It has to do with parsing the input file names. I have a fairly large number of files, I want to do some operations on them in a pairwise fashion (every file has a pair). The names are in the following pattern, with the pairs of files named with _1 and _2 , the... (4 Replies)
Discussion started by: newbie83
4 Replies

2. Shell Programming and Scripting

Looping through Files

Hi all , I am new on this forum . I have to face a particoular implementation issue and I need some help . Requirement : I need to read a particoular file (an xml file) and after reading it I need to call an Oracle Stored Procedure passing the content of the file as paramenter , in order... (3 Replies)
Discussion started by: Kolas79
3 Replies

3. Shell Programming and Scripting

Not looping or creating files

So my script is supposed to repeat for every server in my file, but as of now it is getting stuck on my awk commands # Read file cred.txt (with one IP per line), connect to servers (one at a time), and download directory listing i=1 param=$(sed -n "{$1}p" $parm_dir/cdm_param.txt) #Get the last... (6 Replies)
Discussion started by: MJCreations
6 Replies

4. Shell Programming and Scripting

Conditional Looping In Files

I have a req. where i need to read data from multiple files and take counts of row which satisfy the condition. e.g.: FILE1: Col1 Col2 Col3 12 ab cd 15 de fg 25 gh tm FILE2: Col1 Col2 Col3 21 ab1 cd1 13 de1 fg1 25 gh1 tm1 --- --- FILE-N... i need to find the count of rows... (6 Replies)
Discussion started by: kunal007
6 Replies

5. Shell Programming and Scripting

looping through files with different extensions

Hi all, I am trying to make a for loop invoking files with different extensions (*.ugrd and *.vgrd) and I cant just make it work. Cant figure out how to load the files so as to use them in subsequent commands like the ones in this pseudo code. the files are arranged such that in one date for... (8 Replies)
Discussion started by: ida1215
8 Replies

6. Shell Programming and Scripting

looping through files

I am writing a ksh which has to load 7 files(.dat files) from input directory into oracle tables using sql loader. The process has to take each file at a time and once if it is loaded succesfully using sql loader into oracle tables then the process has to pick next file and load it into oracle... (2 Replies)
Discussion started by: vpv0002
2 Replies

7. Shell Programming and Scripting

Looping through 2 files simultaneously

Hi all, I'm having a problem with a script which should ultimately provide a filename by reading a value from file1 and file2 then join together. I'm planning to use a loop/ loops to get the values out of both files and create a single string unfortunately the code currently treats the second... (7 Replies)
Discussion started by: chris01010
7 Replies

8. Shell Programming and Scripting

Looping on a list of files...

This isn't working for multiple files. It works for one file though. exists1=$(ls | grep gspp*) for FILES in $exists1 do echo "Loading $exists1" ... (23 Replies)
Discussion started by: lazerfoursix
23 Replies

9. Shell Programming and Scripting

Help looping through files, please...

Okay... I've solved one problem. Here's the next. I'm writing a script file that needs to go through a directory and list all files in that directory. I'm using TCL/TK. I figured out how to go through the directory and how to loop through it, but I ran into a little problem. ... (2 Replies)
Discussion started by: kapolani
2 Replies

10. Shell Programming and Scripting

looping files

Hi, I have a file a.lst which lists all files. as a.dat b.dat c.dat I want to process these files mentioned in the list file in a loop. Say I want to display only the first line of all the files a.dat , b.dat, c.dat. How can I go about it? Please help. (5 Replies)
Discussion started by: dharmesht
5 Replies
Login or Register to Ask a Question