Loop to process 2 files with same name in different path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop to process 2 files with same name in different path
# 1  
Old 10-08-2011
Loop to process 2 files with same name in different path

Hello forum members,

I hope you can help me with this I don't know hot to reach.

I have a list of files in "/home/MyPath1/" and in "/home/MyPath2/". The files have the same name in both folders.
(but different content, the content doesn't matter here I think)
/home/MyPath1/
filename1.txt
filename2.txt
filename3.txt
.
.
.
filenameN.txt
/home/MyPath2/
filename1.txt
filename2.txt
filename3.txt
.
.
.
filenameN.txt
I've written a bash script that process a couple of these files at the same time, let say that to get output1.txt the script needs to take
filename1.txt of both folders, then takes filename2.txt of both folders to get output2.txt and so on, the script is as follow:
Code:
# Begin of code
FilePath1=/home/MyPath1/filename1.txt
FilePath2=/home/MyPath2/filename1.txt

.
.
here goes my code that uses both files to get the output.
.
.
# End of code

I want to set a loop that uses as input couple of files from both folders to get one output without enter manually for each process.

May you help with suggestion in how to do this.

Much appreciated in advance.

Grettings
# 2  
Old 10-08-2011
what you are trying to do with that two files to produce output files ..
# 3  
Old 10-09-2011
Hi,

The code needs to process filename1 from folder1 and filename1 from folder2 to produce output1, filename2 from
folder1 and filename2 from folder2 to produce output2 and so on.

I need to process both at the same time because the output from file of folder1 is concatenated to output from file
of folder2.

I have the script that does that, but I have to insert path of both files manually.

So, I need a loop process all couples of files from 2 folders and for each process generate its
corresponding output file.

Something like this:
Code:
For each in folder1
do
# Begin of my current code
FileFolder1=/home/MyPath1/ ${each}
FileFolder2=/home/MyPath2/ ${each}

cat <<-RUTINE_FILE1
some line
sed 'some code' ${FileFolder1}
some line
RUTINE_FILE1

cat <<-RUTINE_FILE2
some line
awk '{some code}' ${FileFolder2}
some line
RUTINE_FILE2
# End of my current code

 Print ${each}.out
done

I think the loop as above would work, but with the structure of my code, I don't know how to create an output
(line in red) for each couple of files.
I mean, create output1 when the loop processes filename1 from both files
and output2 when loop processes filename2 from both files.

Hope somebody could help me.

Much appreciated in advance.

Last edited by Ophiuchus; 10-09-2011 at 01:21 AM..
# 4  
Old 10-09-2011
Something like this?

Code:
#!/bin/bash
for file in folder1/*
do
        file_name=$(basename $file)
        #do something with folder1/file1
        echo folder1/$file_name > $file_name.output
        #do something with folder2/file1
        echo folder2/$file_name >> $file_name.ouput
done

--ahamed
# 5  
Old 10-09-2011
Hi ahamed,

works exactly as I need. I think with cat "<<-..." is not possible in this way. Echo was the solution.

Many thanks for your reply.

Thanks a lot

Grettings
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Process files in loop which have spaces in name

I have a folder with files and I have to process them in a loop. However the filenames have space characters, so the list get split. $ touch "File Number_1" $ touch "File Number_2" $ ls "/tmp/File Number"_* /tmp/File Number_1 /tmp/File Number_2 I tried following (sorry for using the... (3 Replies)
Discussion started by: Wernfried
3 Replies

2. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

3. Shell Programming and Scripting

How to find the path of process in sunOS?

Please support for this issue, I have a below process running on my machine 16108 /usr/jdk/instances/jdk1.6.0/bin/java -Xms3072m -Xmx3072m -Dcbcm.root=/java3_app 20992 java -Djob.name=SOME_JOB_IS_RUNNING -Dcbcm.root=/java3_apps1/cbcm_js/j For PID 16108 I can see that its... (3 Replies)
Discussion started by: mirwasim
3 Replies

4. UNIX for Dummies Questions & Answers

Way to get Code/path , which is executing under a process?

Dear All, Please help me in finding solution for below problem. I need a command or script to get code or path(from which location code is being executed), which is executing under a process ID. I dont have google access here,Please help me in finding solution. Thank you. (3 Replies)
Discussion started by: subbarao12
3 Replies

5. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files: for i in 250.*; do || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

[SOLVED] for loop to process files

I need to process a dirtree containing ms office files such that each file is stored as a variable and also, just the file file stem. Why? They will be using as input and output parameters for another script. For example /path/to/second_script -i filename.docx -o filename Here's what I... (1 Reply)
Discussion started by: graysky
1 Replies

7. AIX

tprof, truncate the process path

Hi, i tryed tprof -skex sleep 6 but... Process PID TID Total Kernel User Shared Other Java ======= === === ===== ====== ==== ====== ===== ==== wait 57372 77863 31.31 31.31 0.00 0.00 0.00 0.00 wait ... (1 Reply)
Discussion started by: zanac
1 Replies

8. Shell Programming and Scripting

Renaming of files with different extensions on the same path to .found with the help of loop

hi , I have certain files at the same path with differeent extensions like .dat , .txt etc ...........i need to rename them with extension .found at the same path with the help of loop.... also the files names will be like this ; abc_2010_c1.dat abc_2010_c2.dat xyz_2010_c1.txt (2 Replies)
Discussion started by: amitpta
2 Replies

9. Programming

How to pass FIFO path to client process ?

Hello everybody ! I have a first program, called "server" which build 2 FIFO's in this way: ... #define PERMS 0666 #define FIFO1 "\tmp\cerere" #define FIFO2 "\tmp\raspuns" ... mkfifo(FIFO1, PERMS) mkfifo(FIFO2, PERMS) ... I want to access these FIFO's in a second separate program,... (1 Reply)
Discussion started by: Ametis1970
1 Replies

10. AIX

loop through the directory for files and sort by date and process the first file

hello i have a requirement where i have a direcotry in which i get files in the format STOCKS.20080114.dat STOCKS.20080115.dat STOCKS.20080117.dat STOCKS.20080118.dat i need to loop through the directory and sort by create date descending order and i need to process the first file. ... (1 Reply)
Discussion started by: dsdev_123
1 Replies
Login or Register to Ask a Question