LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-27-2011, 09:16 AM   #1
Boel
LQ Newbie
 
Registered: Oct 2011
Posts: 1

Rep: Reputation: Disabled
Loop trough list of files, two at the time


Hey,

I have a directory of files, named:
File_1_1, File_1_2, File_2_1, File_2_2, etc..

I want to loop trough the files in this directory pairwise

Code:
for file1 & file2 in /path/to/dir/*
do
 execute command on $file1 and $file2
done
How can I accomplish this? It is important that File_1_1 i paired with File_1_2.

Thanks a lot!
 
Old 10-27-2011, 09:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Maybe try a while loop and perform second read inside the loop
 
Old 10-27-2011, 09:38 AM   #3
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
This seems to work if run in the directory containing the files.
Code:
#! /bin/bash

for fn in *_1
do
  if [ -f "${fn%%_1}"_2 ]; then
    echo $fn, ${fn%%_1}_2
  fi
done
 
1 members found this post helpful.
Old 10-27-2011, 03:13 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
As long as the number of files is not unreasonably large, you could load them into an array, then can call them by address number.

Code:
array=( * )

for (( i=0; i<=${#array[@]}; i+=2 )) ; do

	echo "${array[i]} ${array[i+1]}"

done
It assumes that the files sort out properly when globbed though. Try running "echo *" first to see what order the shell puts them in.

Even better would be to use two arrays, loaded by globs that match the paired filenames.

Code:
array1=( File_*_1 )
array2=( File_*_2 )

for i in "${!array1[@]}"; do

	echo "${array1[i]} ${array2[i]}"

done
Assuming the files are all named in the format you posted, they should match up. Do note that filenames with numbers in them sort as ascii strings, rather than integers, so File_10_1 would come before File_1_1. But at least the pairings will come out the same.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: Unzip files/folders from archive one at a time using a loop cade Programming 3 05-16-2010 11:48 PM
Iptables - Ban a list of ips trough a txt... dan83 Linux - Newbie 41 03-07-2009 03:39 PM
Help setting the time trough the command line v@ny@ Linux - Newbie 3 12-10-2005 01:38 AM
Most efficient way to list files changed since a specific time dman65 Linux - General 4 04-07-2005 01:52 PM
How to List files between date1 and date2 (range of time)? mystique98ls Linux - Newbie 2 09-10-2004 05:36 PM


All times are GMT -5. The time now is 04:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration