Bash scripting issue
Hi,
So what I'm trying to do is get a list of filenames from a directory, then group those files according to any common prefixes and store those groupings in an array. I'm stuck on a certain line, but it very well could be I'm just going about this the wrong way. Here's what I have so far: Code:
DIR_PATH=$1; What am I doing wrong? THanks!! |
Figured it out
I figured it out. When I was echoing the $FILES string to pipe to sed or grep, I hadn't put double quotes around it. This means that spaces were not being preserved!
Correct lines: Code:
group=$( echo "${FILES}" | grep "${prefix}" ); |
Hi, welcome to LQ,
Good on yah for sorting it yourself, and thanks for posting back your findings! Cheers, Tink |
I know you have solved your initial issue but I thought I would give a little feedback on what you have as it confused me
a little. Quote:
Code:
FILES=$( ls $DIR_PATH/*.$EXT | xargs -n1 basename ) Also the assumption here is no file names have spaces. To actually be an array you need another set of parenthesis, like so: Code:
FILES=($( ls $DIR_PATH/*.$EXT | xargs -n1 basename )) Code:
FILE_COUNT=$( echo $FILES | wc -w ) Code:
prefix=$( echo $FILE | cut -d '.' -f1 ); #this correctly gets everything up to first '.' Code:
prefix=${FILE%.$EXT} Code:
FILES=$( echo "${FILES}" | grep -Ev "${prefix}*\.${EXT}" ); the rest of the program to be very wrong (just a thought) |
Hi Grail
Thanks for all your thoughts on the matter. I'm afraid that comment was a bit misleading however, as the array I was creating was further down the line. You are right that I was trying to modify the looping condition variable to affect the number of loops made. And this was a flawed decision.
Having finally got the whole script working, I realized I was making things a lot more complicated than I needed to. As often happens, the end result was very simple and took very few lines (though I'm sure someone could simplify further): Code:
FILES=$( ls $DIR_PATH/*.$EXT | xargs -n1 basename ); Finally, the reason for using cut instead of bash substitution is that the file names WILL have multiple dots. What I'm doing here is taking files like: xxx.yyy.txt xxx.zzz.txt xxx.aaa.txt jjj.bbb.txt jjj.txt and aggregating all lines from the files with the 'xxx' prefix into one file, say, xxx.aggregate.txt, then doing same for 'jjj', etc. Is there a way to get just the 'xxx' using bash substitution? Thanks again for your kind response. |
Hi tubaboy
Sorry this took so long but i was on holidays when I posted and have only just got back to a computer :) Yes substitution is not an issue and here is another alternative for you to look at: Code:
#!/bin/bash |
All times are GMT -5. The time now is 06:31 AM. |