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? |
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"}' |
@colucix
Simply superb !! I need sometime to digest this deliciious meal though :) |
Just for something slightly different:
Code:
echo Oneword | awk -F "" '{q="\047";$1=$1; print q$0q",\\0"q}' OFS="','" |
@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 |
I will contribute a sed solution.
Input file: Code:
nowisthetime Code:
sed "s/./&\',\'/g;s/^/\'/;s/$/\\\0\'/" $InFile > $OutFile Code:
'n','o','w','i','s','t','h','e','t','i','m','e','\0' 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 |
Absolutely fantatstic solution Daniel :)
|
All times are GMT -5. The time now is 12:03 PM. |