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
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
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> _