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.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > comma delimited in a script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-23-04, 14:05
markjason markjason is offline
Registered User
 
Join Date: Jun 2004
Posts: 46
Post 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.
Reply With Quote
  #2 (permalink)  
Old 06-23-04, 14:23
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
sed -e 's/./&,/g;s/,$//g' file
Reply With Quote
  #3 (permalink)  
Old 06-23-04, 14:57
markjason markjason is offline
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
Reply With Quote
  #4 (permalink)  
Old 06-23-04, 15:12
vgersh99 vgersh99 is offline
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" : ",");
}
Reply With Quote
  #5 (permalink)  
Old 06-23-04, 15:27
Damian Ibbotson Damian Ibbotson is offline
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).
Reply With Quote
  #6 (permalink)  
Old 06-23-04, 15:32
vgersh99 vgersh99 is offline
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
Reply With Quote
  #7 (permalink)  
Old 06-23-04, 15:39
fla5do fla5do is offline
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.
Reply With Quote
  #8 (permalink)  
Old 06-23-04, 16:18
markjason markjason is offline
Registered User
 
Join Date: Jun 2004
Posts: 46
Thanks guys, for your replies...
Reply With Quote
  #9 (permalink)  
Old 06-23-04, 16:37
fla5do fla5do is offline
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.
Reply With Quote
  #10 (permalink)  
Old 06-24-04, 15:34
markjason markjason is offline
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On