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
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
11-18-2009, 02:19 PM
#1
LQ Newbie
Registered: Nov 2009
Posts: 3
Rep:
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.
11-18-2009, 02:21 PM
#2
Member
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182
Rep:
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 .
11-18-2009, 02:25 PM
#3
LQ Newbie
Registered: Nov 2009
Posts: 3
Original Poster
Rep:
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.
11-18-2009, 02:39 PM
#4
Member
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182
Rep:
Your code is incomplete, and has something wrong.
You can find the lenght of a string by writing:
The further information can be found in
Advanced Bash Scripting guide in section
Manipulating strings .
11-18-2009, 02:42 PM
#5
Guru
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 16,552
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.
11-18-2009, 03:06 PM
#6
Member
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182
Rep:
Quote:
Originally Posted by
pixellany
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.
11-18-2009, 03:39 PM
#7
LQ Newbie
Registered: Nov 2009
Posts: 3
Original Poster
Rep:
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
11-20-2009, 04:27 AM
#8
LQ Newbie
Registered: Nov 2009
Posts: 2
Rep:
Code:
$ echo apple | grep -o .
a
p
p
l
e
11-20-2009, 07:19 AM
#9
Guru
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 16,552
Quote:
Originally Posted by
#!/usr/bin/which
Code:
$ echo apple | grep -o .
a
p
p
l
e
Wow!!---that's one I would not have thought of.
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 01:48 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News