 |
06-16-2008
|
#1 (permalink)
| | Just Joined!
Join Date: Jun 2008
Posts: 4
| how to pick out parts of a directory name? Hi,
I'm having to teach myself bash scripting for my research, and so far I've been able to manage simple scripts on my own, but now I am stumped. Perhaps someone here can point me in the right direction, because I freely admit I'm a total newbie
I have a bunch of directories filled with multiple subdirectories, the names of which are all numbers. Five of these subdirectories are always named "08*" where the * could be any random digit. (Yes, I know this is stupid, but it's what the MRI scanner outputs.)
I need to make a script that finds these five "08*" subdirectories, then figures out the last digit. Because then I need to sort the digits in ascending order and assign them to variables. I've read stuff about picking out parts of filenames, but not directory names...
Any help would be greatly appreciated! |
| |
06-17-2008
|
#2 (permalink)
| | Linux Engineer
Join Date: Feb 2005
Posts: 982
| If the directories are always 3 digits, and are in the current directory: Code: ls -d 08? | sed 's/[0-9]$/ &/' | while read a b
do
echo $a $b
done I'll leave you to fill in the rest of the loop.
Edit: just realised sort can do it all: Code: ls -d 08? | sort --key=1.3 |
| |
06-17-2008
|
#3 (permalink)
| | Linux User
Join Date: Aug 2006
Posts: 334
| actually, ls sorts the numbers in order, so the sort can be omitted. It could simply be |
| |
06-17-2008
|
#4 (permalink)
| | Just Joined!
Join Date: Jun 2008
Posts: 4
| Quote:
Originally Posted by scm If the directories are always 3 digits, and are in the current directory: Code: ls -d 08? | sed 's/[0-9]$/ &/' | while read a b
do
echo $a $b
done I'll leave you to fill in the rest of the loop. | Thank you! I tried reading the man page for sed to figure out exactly what it's doing, but I'm not entirely clear. (Like I said - total newbie, except for that pascal class back in high school...)
So, it's taking the last digit from my directories, then sending it to sed, then trying to match it from 0-9, then I get lost. It's assigning 08 to a, and the last digit to b, but how does it know which strings to read into a versus b? b seems to be the piece I need and it seems to be reading the values in ascending order. (I guess because ls automatically sorts it.)
Now I'd like to assign each value it gets for b into separate variables. (So if my directories are 084, 085, 086, 087, 088, it would be R1=4, R2=5, R3=6, etc.) How do I do that?
Edited to add:
Duh, I could put a little counter and then do subscripted variables inside the while loop, right? (Slowly the stuff I learned in pascal all those years ago is coming back to me) But now I have to go to a bunch of meetings and can't try it until this evening...
Last edited by magnetchick; 06-17-2008 at 04:30 PM.
Reason: duh
|
| |
06-18-2008
|
#5 (permalink)
| | Linux Engineer
Join Date: Feb 2005
Posts: 982
| Quote:
Originally Posted by magnetchick Thank you! I tried reading the man page for sed to figure out exactly what it's doing, but I'm not entirely clear. (Like I said - total newbie, except for that pascal class back in high school...)
So, it's taking the last digit from my directories, then sending it to sed, then trying to match it from 0-9, then I get lost. It's assigning 08 to a, and the last digit to b, but how does it know which strings to read into a versus b? | It's sending the whole 3 digit name to sed, which is splitting the last digit off and outputting the line minus the last digit, followed by a space, then the last digit (the $ anchors the pattern to the end of the line). This is then fed in the while loop's read which put each "field" into successive variables, hence the first field ("08") goes into a and the second field goes into b.
Regarding the variables, you'd be better off using an array since you have a variable number of directories - read the bash man page for details of how to do that (yeah I know it's a long page, but you have to do some work for yourself!). Or it may be more expedient to deal with each name within the loop rather than trying to cope with them all of a piece - if you give us more detail of what you're trying to achieve we may come up with an elegant, simple solution.  |
| |
06-18-2008
|
#6 (permalink)
| | Just Joined!
Join Date: Jun 2008
Posts: 4
| Quote:
Originally Posted by scm It's sending the whole 3 digit name to sed, which is splitting the last digit off and outputting the line minus the last digit, followed by a space, then the last digit (the $ anchors the pattern to the end of the line). This is then fed in the while loop's read which put each "field" into successive variables, hence the first field ("08") goes into a and the second field goes into b.
Regarding the variables, you'd be better off using an array since you have a variable number of directories - read the bash man page for details of how to do that (yeah I know it's a long page, but you have to do some work for yourself!). Or it may be more expedient to deal with each name within the loop rather than trying to cope with them all of a piece - if you give us more detail of what you're trying to achieve we may come up with an elegant, simple solution.  | OK, here's what I'm trying to do:
I've got a bunch of raw data from an MRI scanner. Each session on the MRI scanner consists of multiple experiments which generates a set of image files, and we call such a set a "run." The scanner outputs the data in a bunch of directories, all named with three digit numbers. If there are 5 runs, then there will always be five directories named 08*. The * is how you tell which run the other folders correspond to. It makes more sense with an example:
Say you have the folders 083, 085, 086, 087, 088. So 083 is Run1, so any other folder ending in 3 contains files for Run1. The scanner will put the first 1000 images in 003, the next 1000 images in 023, the next 1000 in 043, and so on (adding 20 to the folder name each time)
I need to be able to tell my analysis program to grab the data from the folders ending in 3 and manipulate them together. For example:
to3d -prefix Run1 -time:zt 23 188 2000 seq+z **3/I\*
this will take all of the files in directories that end in 3 and put them in 3-D space, and attach "Run1" to the name of the resulting files. Then I need to do it for the next 4 runs.
The next session may have Run1 files in folders ending in 4, so I need to pick out the last digit of the 08* folders for each session... |
| |
06-18-2008
|
#7 (permalink)
| | Linux Enthusiast
Join Date: Mar 2007
Posts: 523
| Well, if you're playing around with variables like that, [url=http://bashcurescancer.com/10-steps-to-beautiful-shell-scripts.html]this[.url] might give you some useful tips.
__________________
Can't tell an OS by it's GUI
|
| |
06-23-2008
|
#8 (permalink)
| | Just Joined!
Join Date: Jun 2008
Posts: 4
| Quote:
Originally Posted by scm It's sending the whole 3 digit name to sed, which is splitting the last digit off and outputting the line minus the last digit, followed by a space, then the last digit (the $ anchors the pattern to the end of the line). This is then fed in the while loop's read which put each "field" into successive variables, hence the first field ("08") goes into a and the second field goes into b.
Regarding the variables, you'd be better off using an array since you have a variable number of directories - read the bash man page for details of how to do that (yeah I know it's a long page, but you have to do some work for yourself!). Or it may be more expedient to deal with each name within the loop rather than trying to cope with them all of a piece - if you give us more detail of what you're trying to achieve we may come up with an elegant, simple solution.  | Can someone please tell my why this doesn't work? I'm trying it to put each b into an array called rn, but apparently it's not putting any data into the array. Thanks... Code: #!/bin/bash
declare i=0
declare -a rn
declare -a test
ls -d 08? | sed 's/[0-9]$/ &/' | while read a b
do
echo $b
let i=i+1
let rn[i]=$b
echo $i
done
echo "Run1 is "${rn[1]}
echo "Run2 is "${rn[2]}
echo "Run3 is "${rn[3]}
echo "Run4 is "${rn[4]}
echo "Run5 is "${rn[5]}
test[1]=blah
echo ${test[1]} |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT. The time now is 05:30 AM. |
| |