[SOLVED] getting directory name with blank spaces into variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
getting directory name with blank spaces into variable
I have build a menu into my script that goes something like this:
Code:
jorge@nixlap ~/Music $ ls -d */ | cat -n | sed 's/\///g' 2>/dev/null
1 adele
2 Jay-Z-Unplugged
3 Orishas-A lo cubano
4 Orishas-Emigrante
now, the user would enter the index number of the artist/album, and that number is passed to a function to get the artist name, all is well but when the folder contins blank spaces I'm running into trouble. I'm only getting the first word of folder. This is what I'm using to get the folder name
It's the "print $2" in your awk that's causing the problem. "awk '{print $2}'" prints the second column in the row using whitespace for a delimiter, which means if there is any white space in your artist name, it's just going to grab the first word (awk '{print $3}' would grab the second, $4 the third, etc.). You'll need to re-think how you're grabbing the artist name. Maybe instead of "print the second column" you should "print everything after the first column".
It's the "print $2" in your awk that's causing the problem. "awk '{print $2}'" prints the second column in the row using whitespace for a delimiter, which means if there is any white space in your artist name, it's just going to grab the first word (awk '{print $3}' would grab the second, $4 the third, etc.). You'll need to re-think how you're grabbing the artist name. Maybe instead of "print the second column" you should "print everything after the first column".
yes, I had already realized that... but should I stick with awk and explore how to do that, or would it be easier by other command?... I only know how to do the basic awk manipulation...
I was trying to point you in the right direction so you could find the answer yourself and learn something from this experience. Something something teach a man to fish...
Last edited by suicidaleggroll; 03-24-2014 at 10:31 AM.
Did you try looking up my suggestion? select in bash can easily accomplish the task you are after. Admittedly you will get the terminating '/' after each directory name, but its a start
and much much simpler.
Did you try looking up my suggestion? select in bash can easily accomplish the task you are after. Admittedly you will get the terminating '/' after each directory name, but its a start
and much much simpler.
No, I didn't even use awk.... I used this command which did the work just the same
That was why I suggested switching to perl - it is much easer to get quoting things right, and the different syntax allows for much more complex things to be done easily.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.