Field separator

Hello All,
I have a file, but I want to separate the file at a particular record with comma"," in the line
Input file
APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US
to
Output file
APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US

This is for Bash/Ksh shell.

Any help is much appreciated.

And how do you tell PRICEPERPIECE,DOLLAR600EACH from PRICE,PERPIECEDOLLAR600,EACH ?

I was thinking of using awk -F to separate the fields once I know where to cut then using cut command.

But if there is a single command that I can use like awk or sed to add the commas at the above given positions that will be great.

Again - how are the single fields to be identified?

Oh! that will be constant, like
APPLE5S, 7 characters constant ( it could be APPLE4S,5S or 6S, but it will NOT be APPLE4 or 5 or 6), same with other separation (like SAMSUNGS2,S3,S4,S5 it will NOT be SAMSUNG4 or 5 or 6 etc..,) similarly 010020340URX581949695 number could change but the no of characters would remain same.
So, what I wanted exactly is a comma separation at below records
7,16,29,50,52

I hope this i making sense.

Thanks in advance.

You may want to adapt the proposal for a very similar problem in this post.

1 Like

Thankyou RudiC! I will give it a shot.

I you have GNU awk 4.x, try:

gawk '{$1=$1}1' FIELDWIDTHS="7 9 13 14 20 2" OFS=,

If the fields definition is in a variable, try

awk '
NR == 1         {n=split(F, Pos, ",")}
                {for (i=n; i>0; i--) $(Pos)=$(Pos) "," }
1
' F=$FIELDS FS="" OFS="" file

If in a file, try

awk '
NR == 1         {n=split($0, Pos, ",")
                 next}
                {for (i=n; i>0; i--) $(Pos)=$(Pos) "," }
1
' fieldsfile FS="" OFS="" file

gawk, mawk, BSD awk:

awk -v F="7 16 29 43 63" 'BEGIN{split(F,P); FS=OFS=x} {for(i in P) $P=$P ","}1' 

--
Just realized it is the same approach as RudiC's but a different execution...

As your lines are constant and delimiters are at hard positions then this simple hard coded method might be OK, longhand using builtins:-
OSX 10.7.5, default bash terminal.

#!/bin/bash
echo 'APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US
APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US
APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US' > /tmp/text.txt
while read line
do
	echo "${line:0:7},${line:7:9},${line:16:13},${line:29:13},${line:42:21},${line:63}"
done < /tmp/text.txt > /tmp/text.csv
cat /tmp/text.csv

Results:-

Last login: Wed Jun 10 18:58:31 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 csv.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./csv.sh
APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US
APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US
APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US
AMIGA:barrywalker~/Desktop/Code/Shell> _
sed -e 's/^.\{63\}/&,/' -e 's/^.\{43\}/&,/' -e 's/^.\{29\}/&,/' -e 's/^.\{16\}/&,/' -e 's/^.\{7\}/&,/'

--
GNU sed -e; BSD sed -E :

gsed -r 's/^.{63}/&,/;s/^.{43}/&,/;s/^.{29}/&,/;s/^.{16}/&,/;s/^.{7}/&,/