LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 08: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, 08:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Maybe try a while loop and perform second read inside the loop
 
Old 10-27-2011, 08:38 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
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, 02:13 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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



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
Iptables - Ban a list of ips trough a txt... dan83 Linux - Newbie 42 05-20-2019 05:42 PM
BASH: Unzip files/folders from archive one at a time using a loop cade Programming 3 05-16-2010 10:48 PM
Help setting the time trough the command line v@ny@ Linux - Newbie 3 12-10-2005 12:38 AM
Most efficient way to list files changed since a specific time dman65 Linux - General 4 04-07-2005 12:52 PM
How to List files between date1 and date2 (range of time)? mystique98ls Linux - Newbie 2 09-10-2004 04:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:12 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
Open Source Consulting | Domain Registration