LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 10-11-2004, 10:38 AM   #1
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Rep: Reputation: 15
exploding string into individual characters using a shell script


I am having trouble exploding a URL into a list of individual characters into a file using a linux bash script.

Example:
p.t.com >> Into:
p
.
t
.
c
o
m

I fiddled around with it yesterday and I guess i am having a brain malfuction. Can someone help me out? I'd be forever in your service.
Thanks,
famerjoe
 
Old 10-11-2004, 10:42 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
If there's no space in your URL, then maybe this:
Code:
echo "$URL" | sed 's|\([^[:space:]]\)|\1 |g' | sed 's| |\n|g'
Yves.
 
Old 10-11-2004, 10:53 AM   #3
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
thanks so much. Worked like a dream!

-farmerjoe
 
Old 10-11-2004, 03:46 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,494

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
echo "$URL" | sed 's|\([^[:space:]]\)|\1 |g' | sed 's| |\n|g'
Or simpler:
Code:
echo "$URL"  | sed 's/./&\n/g'
 
Old 10-12-2004, 03:16 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
I like this!
But you're (unknowingly) pointing at something I've always wondered: If you write a sed rule like 's|(\something\)|\1 something|g' (note that something is *after* the \1, and the 'g' option), is there a risk on any platform, that sed will apply the same rule to the something that is after \1? Or will parsing go on with what is after the initial \(something\) consistently on all platforms?
In short, is there a risk to write a rule that will infinitely recurse?

Yves.
 
Old 10-12-2004, 04:30 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,494

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Well, If such a bug existed, it would have manifested itself since 1978, where the first sed was made available (Unix version 7), and I'm not aware of that.

Another side note, both my answer and yours are in fact non portable, as they use a gnu extension allowing \n to be present in the replacement string.
With standard Unix sed, one should use something like:
Code:
echo "$URL"  | sed 's/./&\^J/g'
Where ^J is a real newline, entered as is or with the sequence control-V control-J.

PS: Il fait beau à Rennes ?
 
Old 10-12-2004, 07:21 AM   #7
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Hi! Weather's changing from bright sunshine to heavy rain, and back, every 10 minutes! But à Rennes, je ne sais pas...

I'm very interested in that last bit (not the weather, the ^J )
How did you know control-V control-J would do that?

Yves.
 
Old 10-12-2004, 12:23 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,494

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Je suis tombé dedans quand j'étais petit ...
 
Old 10-13-2004, 02:59 AM   #9
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Ah... OK. (non French people: he fell into Unix when he still was a kid, reference to Obelix in the Asterix books)

Is there a man or web page where I could drink some of this magic knowledge?
 
Old 10-13-2004, 03:23 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,494

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
man vi:
^V: quote non-printable character
man ksh/bash:
set -o
vi Use a vi-style command line editing interface.

Yes you have to read between the lines to know all of that ...
 
  


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
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
shell script: grep using string with specail charactors? Chowroc Programming 6 11-14-2005 08:47 PM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
change case of a string (TOUPPER) in shell script raees Linux - Software 3 05-03-2005 02:13 PM


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

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