LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-18-2011, 11:01 AM   #1
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Script Help "Line Spliting"


I am trying to create a list of files in a group of directories and chop off the extensions. However when I try it splits into lines. I tried quoting the list "$(ls -R ~joe/Videos)" but then my line cutting off the extension does not work.

I feel like I should knwo what I am doing wrong but I can't seem to figure it out....

Quote:
#! /bin/bash
clear
figlet Video List
for files in $(ls -R ~joe/Videos)
do
echo "${files%%.*}"
done


All Help is Appreciated !!

Last edited by jv2112; 12-18-2011 at 11:03 AM.
 
Old 12-18-2011, 11:21 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Using "${file%%.*}" will remove everything right of the first period. Not just the extension if a filename contains multiple periods.
Use "${file%.*}" instead.

Another problem may be due to white space in names. A filename with a space in it could be split into two arguments.
Here is a howto on different ways to deal with them using for and while loops:
http://www.cyberciti.biz/tips/handli...s-in-bash.html
So you could use
Code:
ls -- . | while read file; do echo "${file%.*}"; done
Sometimes, I will use find to construct lines of bash code, using the -printf find command. Then I can create a line of code and add quotes around the filename. This works well, over using -exec, if different forms of the filename are used more than once in the command.

Last edited by jschiwal; 12-18-2011 at 11:38 AM.
 
1 members found this post helpful.
Old 12-18-2011, 11:27 AM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by jschiwal View Post
Sometimes, I will use find to construct lines of bash code, using the -printf find command. Then I can create a line of code and add quotes around the filename.
Neat idea!
 
Old 12-18-2011, 01:34 PM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Although...

While I prefer to do everything possible in BASH when scripting (or move it to PERL is I can avoid all exterior calls that way) I may have a useful suggestion. Have you checked the man pages for 'basename' and 'dirname'?
The 'basename' utility specifically allows you to remove KNOWN extensions, the trick being that you must specify the extension.
If the extensions you expect constitute a large or ill-defined range then you are in for an interesting amount of string manipulation, and will want to do things more efficiently than you could with basename.

Roughly how many files with how many different extensions might we be considering?
 
Old 12-18-2011, 04:20 PM   #5
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Original Poster
Rep: Reputation: 106Reputation: 106
Talking

Thanks for the education and suggestions. The while loop method listed works well. I have about 600 files with about 7-10 different extensions so fewer lines of code the better for this small intermittent task.

----------Thanks again -----------------


Quote:

#! /bin/bash
clear
figlet Collection List
echo -e "\e[00;34mWhat parent directory does your collection start ? \e[00m"
read start
echo
echo -e "\e[00;31m ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \e[00m"
echo
echo -e "\e[00;33m What do you want to call your list ? \e[00m"
read Collection
touch /home/$USER/"$Collection-$(date +%F)"
ls -R $start | while read file; do echo "${file%.*}" >>$Collection-$(date +%F); done
echo -e "\e[00;31m---- Preparing File : $Collection ---- \e[00m"

sleep 5
echo -e "\e[00;31m ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \e[00m"
echo -e "\e[44;37m Do you want to view \e[44;33m$Collection. \e[00;34m--Yes / No - \e[00m"
read ans
case $ans in
Yes | yes) less /home/$USER/"$Collection-$(date +%F)" ;;
* ) figlet Exiting ...
sleep 3
cowsay "Goodbye $USER"
;;
esac


Last edited by jv2112; 12-18-2011 at 08:44 PM.
 
Old 12-19-2011, 04:19 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well apart from not being a fan of using ls to parse data (for reasons outline here), something I found a little odd with your script is the following:
Code:
touch /home/$USER/"$Collection-$(date +%F)"
echo "${file%.*}" >>$Collection-$(date +%F)
Are these supposed to be 2 completely different files in different places?
Also, in the unusual situation that you run this script around midnight, you could end up with differently named files
with your data split over the 2 of them (just a thought)

Something you might like to look at with your yes / no question (apart from handling incorrectly entered options) is
that you can test for a single response by controlling the format.
Example:
Code:
read ans
case ${ans^^} in # alternative is ,,
YES) <do blah>
^^ convert to all upper case and ,, to convert to all lower case
 
1 members found this post helpful.
Old 12-19-2011, 04:57 AM   #7
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Original Poster
Rep: Reputation: 106Reputation: 106
Talking

Thanks for the suggestion on the case statement Grail. I'll hold onto that one for future use. Also thanks for catching the typo.

Works as expected now ........

Quote:
#! /bin/bash
clear
figlet Collection List
echo -e "\e[00;34mWhat parent directory does your collection start ? \e[00m"
read start
echo
echo -e "\e[00;31m ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \e[00m"
echo
echo -e "\e[00;33m What do you want to call your list ? \e[00m"
read Collection
touch /home/$USER/"$Collection-$(date +%F)"
ls -R $start | while read file; do echo "${file%.*}" >>/home/$USER/"$Collection-$(date +%F)"; done
echo
echo -e "\e[00;31m---- Preparing File : $Collection ---- \e[00m"

sleep 5
echo -e "\e[00;31m ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \e[00m"
echo -e "\e[44;37m Do you want to view \e[44;33m$Collection. \e[00;34m--Yes / No - \e[00m"
read ans
case ${ans^^} in
# ^^ convert to all upper case and ,, to convert to all lower case

YES) cat /home/$USER/"$Collection-$(date +%F)" | less ;;
*) figlet Exiting ...
sleep 3
cowsay "Goodbye $USER"
;;
esac


 
Old 12-19-2011, 06:45 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sorry to still be a bit picky (I am old so I have been told some times it is my prerogative ), but your file
construct still has the time issue. My suggestion would be to set it in a variable and use at your leisure or
open the file for input at point of creation. Also, redirecting into the file will create it so the touch is not required either.
An example of the first option would be:
Code:
read Collection

collection_file_name="/home/$USER/$Collection-$(date +%F)"

ls -R $start | while read file; do echo "${file%.*}" >> "$collection_file_name"; done
 
1 members found this post helpful.
Old 12-19-2011, 09:36 PM   #9
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Original Poster
Rep: Reputation: 106Reputation: 106
Thanks for the suggestions grail...
 
  


Reply

Tags
bash



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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Simple bash script "unexpected end of line error" snowman81 Programming 11 11-11-2007 09:31 AM
shell script question: removing a line according to a duplicate "field" mattie_linux Linux - Newbie 1 01-12-2006 03:57 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Can I redirect script output to a file without ">> $LOGFILE" at the end of each line davee Linux - General 1 12-19-2003 05:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration