Topic
3 replies Latest Post - ‏2005-03-09T16:19:10Z by SystemAdmin
SystemAdmin
SystemAdmin
232 Posts
ACCEPTED ANSWER

Pinned topic Exploding a URL into individual characters

‏2004-10-11T15:20:23Z |
I am having trouble exploding a URL into a list of individual characters into a file using a linux bash script.

Example:
http://p.t.com >> Into:
h
t
t
p
:
/
/
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
Updated on 2005-03-09T16:19:10Z at 2005-03-09T16:19:10Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    232 Posts
    ACCEPTED ANSWER

    Re: Exploding a URL into individual characters

    ‏2004-10-22T06:14:22Z  in response to SystemAdmin
    echo "http://p.t.com" |sed 's/\(.\)/\1\n/g'
  • SystemAdmin
    SystemAdmin
    232 Posts
    ACCEPTED ANSWER

    Re: Exploding a URL into individual characters

    ‏2005-03-03T19:03:26Z  in response to SystemAdmin
    Here's a start on it. You will find this easy to do when using sed. An example of what you are doing here:
    [b]# echo HELLO | sed s/./\&\;/g | tr '\;' '\n'[/b]
    [b]H[/b]
    [b]E[/b]
    [b]L[/b]
    [b]L[/b]
    [b]O[/b]

    The 's' command is for substiture, the '.' in the sed command matches the characters, and the '\&' says to print each character matched. The '\;' is added as a delimeter to use tr with and the 'g' at the end to say do it to all matches (global). Then running tr command we translate the '\;' to newline characters (\n).

    There has to be a way to do direct substituion of a '\n' in sed, but I can't seem to find it right off hand. I should probably look it up in a sed/awk manual. The "tr" and "sed" commands work wonders though. It recall that a ';' is not commonly used in URLs, but you can substitute any character in the sed and tr commands for one you know you won't encounter. An example below could be like...

    [b]echo $QUERY_STRING | sed s/./\&\;/g | tr '\;' '\n'[/b]

    which would echo out the query string from a web server if you had that in a cgi script and it would output it in that form.

    Hope this helps,
    John Frakes
  • SystemAdmin
    SystemAdmin
    232 Posts
    ACCEPTED ANSWER

    Re: Exploding a URL into individual characters

    ‏2005-03-09T16:19:10Z  in response to SystemAdmin
    > I am having trouble exploding a URL into a list of
    > individual characters into a file using a linux bash
    > script.
    >
    > Example:
    > http://p.t.com >> Into:
    > h
    ...
    > m

    Well, you said "bash script", not "sed script", isn't it ???
    You may consider:

    S=http://p.t.com
    while ; do R="${S#?}"; echo "${S%%$R}"; S="$R"; done

    Happy bashing!
    ;-)