LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2014, 09:55 AM   #1
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 233

Rep: Reputation: 16
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
Code:
artist=$(ls -d */ | cat -n | sed 's/\///g' | grep $1 | awk '{print $2}')
$1 is the index number pass to function... how could I get the whole dir name when containing blank spaces?
 
Old 03-24-2014, 09:59 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649
Quotes will fix your problem.

Edit: Sorry,.. as in:

Code:
ls "$artist"

Last edited by szboardstretcher; 03-24-2014 at 10:01 AM.
 
Old 03-24-2014, 10:02 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,706

Rep: Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025
May I suggest you have a look at the select command.

As for the command you have used, try placing your output in a file and then retry with your awk command and see what goes wrong.
 
Old 03-24-2014, 10:18 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133
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".
 
Old 03-24-2014, 10:22 AM   #5
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 233

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by suicidaleggroll View Post
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...
 
Old 03-24-2014, 10:29 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133Reputation: 2133
Quote:
Originally Posted by mia_tech View Post
yes, I had already realized that...
Then why didn't you ask the question you really wanted the answer to, how to print all columns except the first one using awk or another tool?

Or better yet, you could google it. A 2 second google search for "awk print all columns except first" yields all of these suggestions:
http://stackoverflow.com/questions/4...field-with-awk
http://stackoverflow.com/questions/2...th-to-the-last
http://stackoverflow.com/questions/2...-three-columns
http://stackoverflow.com/questions/3...except-1-and-2
http://www.unix.com/shell-programmin...first-one.html
http://superuser.com/questions/21339...ntent-except-1
http://www.cyberciti.biz/faq/unix-li...ields-command/
http://objectmix.com/awk/26528-all-c...xcept-one.html
http://www.theunixcode.com/2013/12/u...h-to-the-last/

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.
 
Old 03-24-2014, 11:10 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,831

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
I would suggest switching the application to perl.

Much easier to work with for things like this.
 
Old 03-24-2014, 11:24 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,706

Rep: Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025
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.
 
Old 03-25-2014, 09:45 AM   #9
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 233

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
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

instead of
Code:
artist=$(ls -d */ | cat -n | sed 's/\///g' | grep $1 | awk '{print $2}')
changed to
Code:
artist=$(ls -d */ | head -"$1" | tail -1 | sed 's/\///g')
 
Old 03-25-2014, 10:11 AM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649Reputation: 1649
Huh. So quotes fixed your issue. Nice.
 
Old 03-25-2014, 11:40 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,831

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
Quote:
Originally Posted by szboardstretcher View Post
Huh. So quotes fixed your issue. Nice.
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.
 
Old 03-25-2014, 08:20 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,706

Rep: Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025Reputation: 3025
Well for others who might view the ticket:
Code:
select name in */ EXIT
do
    [[ "$name" == EXIT ]] && break

    echo "You chose artist ${name%/}"
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to check for blank spaces in a string? Micky12345 Linux - Newbie 8 02-27-2012 10:34 AM
xine and blank spaces almcneill Linux - Software 1 10-20-2011 06:51 PM
[SOLVED] SED - how to remove blank spaces carolflb Linux - Newbie 2 01-30-2010 06:02 AM
to display the number of blank spaces in a specified file rajdey1 Linux - Newbie 10 11-25-2008 06:09 PM
Problem with SED and blank spaces BigLarry Programming 2 06-10-2004 04:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:25 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration