LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 11-18-2009, 02:19 PM   #1
iLL_NiNo
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Rep: Reputation: 0
Need help > Horizontal line to vertical Shell script


Hey, I need to do the following:

A program that makes a sentence, echoed in vertical. For eg:

input:
apple
output:
a
p
p
l
e

I need it for a whole sentence not only one word. Please help me out.
 
Old 11-18-2009, 02:21 PM   #2
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
This sounds like homework.
Why don't you tell us what you've tried and we go from there.

Last edited by ammorais; 11-18-2009 at 02:22 PM.
 
Old 11-18-2009, 02:25 PM   #3
iLL_NiNo
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Yes, it is homework. I made the input only, but dont know how to continue, didnt find anything with google.

#!/bin/sh

if [ $# -ne 1 ]
then
echo "Write a sentence: ";
read x;
fi

if [ $# -eq 1 ]
then
x=$1;
fi

h=length($x);

That's the code, only the input.
 
Old 11-18-2009, 02:39 PM   #4
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
Your code is incomplete, and has something wrong.

You can find the lenght of a string by writing:

Code:
LENGHT="${#VAR}";
The further information can be found in Advanced Bash Scripting guide in section Manipulating strings.
 
Old 11-18-2009, 02:42 PM   #5
pixellany
Guru
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 16,552

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
You can use SED to replace every character with itself + a newline. Go here for a really good tutorial on SED:

http://www.grymoire.com/Unix/Sed.html

You'll be using the "s" (substitute) command and a "backreference" to replace the character with itself.
 
Old 11-18-2009, 03:06 PM   #6
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
Quote:
Originally Posted by pixellany View Post
You can use SED to replace every character with itself + a newline. Go here for a really good tutorial on SED:

http://www.grymoire.com/Unix/Sed.html

You'll be using the "s" (substitute) command and a "backreference" to replace the character with itself.

iLL_NiNo, opting for this later approach will certainly impress your teacher.
 
Old 11-18-2009, 03:39 PM   #7
iLL_NiNo
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Solved. THANX!

#!/bin/sh

if [ $# -eq 0 ]
then
echo "Kerem irja be a szoveget: ";
read szoveg;
fi

if [ $# -ne 0 ]
then
szoveg=$1;
fi

h="${#szoveg}";

for i in `seq 0 $h`
do
echo ${szoveg:$i:1};
done
 
Old 11-20-2009, 04:27 AM   #8
#!/usr/bin/which
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 1
Code:
$ echo apple | grep -o .
a
p
p
l
e
 
Old 11-20-2009, 07:19 AM   #9
pixellany
Guru
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 16,552

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
Quote:
Originally Posted by #!/usr/bin/which View Post
Code:
$ echo apple | grep -o .
a
p
p
l
e
Wow!!---that's one I would not have thought of.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Horizontal and Vertical sync rates?? chris24300 Linux - Newbie 5 09-08-2009 05:18 PM
mouse goes vertical; not horizontal (slack) nadroj Linux - Newbie 22 10-30-2006 03:09 AM
NVIDIA - Horizontal/Vertical Adjust JamboUK Ubuntu 4 05-19-2006 06:52 AM
Horizontal and vertical refresh sharpie General 5 01-24-2004 08:17 PM
Horizontal and vertical refresh rate johannes Linux - Laptop and Netbook 3 10-31-2003 06:44 AM


All times are GMT -5. The time now is 01:48 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration