Register
It is currently Fri Nov 28, 2014 7:09 pm

shifting help for a newbie


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Feb 14, 2012 6:32 pm   

Joined: Tue Feb 14, 2012 6:15 pm
Posts: 6
I am extremely green to bash scripting and have googled till my fingers are bleeding. I would like to know how ( I assume the "shift" command) to take a line of an infinite number of words separated by spaces and then output them in the same order except separated by comma's.


I am planning to call the input with an echo and pipe it to all lowercase.

example..... INPUT=RED BLUE GREEN PURPLE BROWN

echo "$INPUT| tr [:upper:] [:lower:] | " I am at a loss as to what to do next."

I need the output to be: red, blue, green, purple, brown



Thanks in advance.


Top
 Profile  
 PostPosted: Tue Feb 14, 2012 8:32 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 574
hi,

Code:
input="RED BLUE GREEN PURPLE BROWN"
input="${input,,}" #to lower case
input="${input// /,}" #space to comma
echo "$input"
red,blue,green,purple,brown
see man bash /^ *parameter expansion


Top
 Profile  
 PostPosted: Tue Feb 14, 2012 11:15 pm   

Joined: Tue Feb 14, 2012 6:15 pm
Posts: 6
Ahhh, I see what you're doing. So I don't need the shift at all.

This worked for me after looking at what you had.

# set my input variable
INPUT="RED BLUE GREEN PURPLE BROWN"

# set my output variable as input then piped it to translate upper
# to lower then piped it to translate spaces to commas
OUTPUT=`echo $INPUT | tr [:upper:] [:lower:] | tr ' ' ', '`

#called my output data
echo $OUTPUT

Thanks a ton Watael. I wasn't expecting such a fast response.
I am sure that I will have tons more questions in the near future.

Have a great day.


Top
 Profile  
 PostPosted: Wed Feb 15, 2012 6:20 pm   

Joined: Tue Feb 14, 2012 6:15 pm
Posts: 6
It's me again.

Do you know of a way to keep the spaces after the comma?
Instead of red,blue,green,purple,brown
get red, blue, green, purple, brown
Thanks again.

EDIT:
Just in case anyone else ever needs this..... This is what worked for me.
Instead of -- OUTPUT=`echo $INPUT | tr [:upper:] [:lower:] | tr ' ' ', '`
I used -- OUTPUT=`echo $INPUT | tr [:upper:] [:lower:] | sed 's/ /, /g'
I think the 's sets the pattern ( 's/this/tothis/) and the g' says to do this to all instances. (basically repeat the pattern)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP