LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-03-2012, 04:49 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
break word into individual alphabets


Hi all,

I am looking "ideally" into a sed one line which would do magic such as follows..

input: Oneword
Output:'O','n','e','w','o','r','d','\0'

At end of each word split, it should have '\0' is there any quick and samrt way with sed or any other language?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-03-2012, 05:10 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
An awk solution:
Code:
$ echo Oneword | awk 'BEGIN{FS=""}{for (i=1;i<=NF;i++) printf "\x27%s\x27,",$i}END{print "\x27\\0\x27"}'
'O','n','e','w','o','r','d','\0'
The Field Separator set to the empty string take charge of splitting the string into individual characters. "\x27" is the hex code of the single quote.
 
2 members found this post helpful.
Old 03-03-2012, 05:22 AM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@colucix

Simply superb !!

I need sometime to digest this deliciious meal though
 
Old 03-03-2012, 09:09 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just for something slightly different:
Code:
echo Oneword | awk -F "" '{q="\047";$1=$1; print q$0q",\\0"q}' OFS="','"
 
2 members found this post helpful.
Old 03-15-2012, 07:07 AM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@colucix and @frail.

This is simply fascinating, I am truly mesmerized by the beauty of awk !

Can you please explain me login in your awk one liners? I am still learning so your help would be highly appreciated.

Thank you
 
Old 03-15-2012, 10:45 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
I will contribute a sed solution.

Input file:
Code:
nowisthetime
forallgoodmen
tocometotheaid
oftheirparty
Code:
Code:
sed "s/./&\',\'/g;s/^/\'/;s/$/\\\0\'/" $InFile > $OutFile
Output file:
Code:
'n','o','w','i','s','t','h','e','t','i','m','e','\0'
'f','o','r','a','l','l','g','o','o','d','m','e','n','\0'
't','o','c','o','m','e','t','o','t','h','e','a','i','d','\0'
'o','f','t','h','e','i','r','p','a','r','t','y','\0'
Explanation:
Starting with this: nowisthetime
we execute this: s/./&\',\'/g
to produce this: n','o','w','i','s','t','h','e','t','i','m','e','

That's a good intermediate result but we need to "dress up" the
beginning and end of each line, so ...

Then we execute this: s/^/\'/
to prefix each line with a single quote mark
to produce this: 'n','o','w','i','s','t','h','e','t','i','m','e','

Finally, we execute this: s/$/\\\0\'
to suffix each line with \0'
to produce the desired result: 'n','o','w','i','s','t','h','e','t','i','m','e','\0'


Daniel B. Martin

Last edited by danielbmartin; 03-15-2012 at 10:50 AM. Reason: Correct t7po
 
2 members found this post helpful.
Old 03-15-2012, 07:36 PM   #7
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Absolutely fantatstic solution Daniel
 
  


Reply



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
Capital letters in alphabets. rblampain General 12 07-27-2011 03:11 AM
Checking for Alphabets gregarion Programming 1 01-25-2010 02:27 PM
SquidGuard: filtering non-english alphabets jlregister Linux - Server 0 11-09-2007 05:33 AM
French accented alphabets..... HELP!!!! arun79 Mandriva 1 03-17-2004 07:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:55 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration