| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

06-23-04, 14:05
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 46
|
|
comma delimited in a script
|
|
 Hi
I have a following requirement
input
-----
sdkfjggs
skdk
tueods
jdkflghfff
ouput
----
s,d,k,f,j,g,g,s
s,k,d,k
t,u,e,o,d,s
j,d,k,f,l,g,h,f,f,f
after certain number of characters in each line(1 character in this case) i should put a comma delimiters... can any one help me do this using script...
Thanks
Mark.
|
|

06-23-04, 14:23
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
|
|
sed -e 's/./&,/g;s/,$//g' file
|
|

06-23-04, 14:57
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 46
|
|
|
|
Thanks for your input
can you explain me what does your command does and also if i have to put comma after 2 chars where should i change.
Thanks
Mark
|
|

06-23-04, 15:12
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
|
|
ok, a bit of a different spin - if you need to change the 'starting' location.
nawk -v start=2 -f mark.awk file
you can change 'start' from the command line - to start from the 5-th char:
nawk -v start=5 -f mark.awk file
here's mark.awk
Code:
# determine the length of a record/line
len=length($0);
# print chars. UP to the 'start' location
printf("%s%s", substr($0,1,start-1), (start >= len) ? "\n" : "");
# loop thorugh the remaning chars (up to 'len') putting ',' in-between
for(i=start; i <= len; i++)
printf("%s%s", substr($0,i,1), (i==len) ? "\n" : ",");
}
|
|

06-23-04, 15:27
|
|
Padawan
|
|
Join Date: Jun 2002
Location: UK
Posts: 525
|
|
Quote:
|
can you explain me what does your command does and also if i have to put comma after 2 chars where should i change.
|
If you mean a comma after evey 2 chars, stick with the sed solution...
sed 's/../&,/g;s/,$//' yourFile > newFile
The '.' is a wildcard representing a single character, the '&' represents the pattern matched matched by the regular expression - in this example '..', i.e. any 2 characters. Overall, you are replacing every 2 characters with those same 2 characters plus a comma (and then you replace the comma that would be at the end of line with 'nothing' to remove it).
|
|

06-23-04, 15:32
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
|
|
I think the OP wanted to start placing commas AFTER a certain number of characters; say after 3 chars:
sdkfjggs
would become:
sdk,f,j,g,g,s
|
|

06-23-04, 15:39
|
|
Registered User
|
|
Join Date: Oct 2003
Location: Germany
Posts: 138
|
|
Try this :
I="marks_inputfile"
O="marks_outputfile"
D="," # Delimiter to use
hop_len="3" # length of the chars you want to split
> $O
cat $I | awk -v hop_len=$hop_len -v O=$O -v D=$D ' BEGIN { FS="@"}
{
str_len=length($1)
x=1
comma_string=""
while ( x < str_len )
{
help_var=substr($1,x,hop_len)
if (x == 1 )
{
comma_string=help_var
}
else
{
comma_string=comma_string""D""help_var
}
x=x+hop_len
}
# output on screen
print comma_string
# output in a file
print comma_string > O
}'
# output from the new file to screen
# cat $O
__________________
Greetings from germany
Peter F.
|
Last edited by fla5do; 06-23-04 at 15:43.
|

06-23-04, 16:18
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 46
|
|
Thanks guys, for your replies...
|
|

06-23-04, 16:37
|
|
Registered User
|
|
Join Date: Oct 2003
Location: Germany
Posts: 138
|
|
Quote:
|
Originally Posted by markjason
Thanks guys, for your replies...
|
which solution you prefer ?
I am interested !
__________________
Greetings from germany
Peter F.
|
|

06-24-04, 15:34
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 46
|
|
Hard to say as per my req as of now i would prefer
sed -e 's/./&,/g;s/,$//g' file
Thanks
Mark.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
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
|
|
|
|
|