Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Thanks for putting me on the right direction! I learned a lot.
Your code didn't really work for a couple of reasons though.
1) The for loop uses spaces as field separators
2) the regex should not be in quotes unless you want it to be literal
I found the solution for the reverse problem (tested with echo in place of mv):
Code:
ls -1 | while read file; do
if [[ $file =~ (200[89])\ (Fall|Spring)(.*) ]]; then
mv "'$file' '${BASH_REMATCH[1]}`echo ${BASH_REMATCH[2]}|cut -c1`${BASH_REMATCH[3]}'";
fi;
done
That was far more difficult than I expected, but I think next time I have something similar I might be able to do it fairly quickly. =)
Note that 'The for loop uses spaces as field separators' is typical for almost all *nix utils ie space is seen as an arg separator, that's why you don't usually see spaces in (pre-supplied) filenames.
Causes too much hassle.
What annoyed me was that neither escaping the spaces (ls -b), nor surrounding the filenames in quotes (ls -Q) worked. The for loop would still separate a the space. It seems to me that quotes should have worked since they aren't allowed in file names, and passed as arguments they would be there to group words.
I'd alter the IFS value to just a newline. By default its space or tab or newline.
You may need to restore it to the std values after the loop if you're going to do more processing in the script. http://tldp.org/LDP/abs/html/internalvariables.html
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.