Inserting commas into file at set locations...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting commas into file at set locations...
# 1  
Old 10-17-2011
Question Inserting commas into file at set locations...

Hey everyone.

What would be the best method to insert commas (or comma-quotes) into a file at set locations.

Every line in the file looks like this:
Code:
 1234567890123456 123456BIGAUDIODYNAMITE33      123.12  123456  12345678901234MARK E WILLIAMS           123456

The comma's should appear at: 11,17,18,20,22,24,40,45,54,62,78,101,103,105,107

Hence the line above should look like (with comma quotes)

Code:
" 1234567890","123456"," ","12","34","56","BIGAUDIODYNAMITE","33   ","   123.12", "  123456","  12345678901234","MARK E WILLIAMS           ","12","34","56

Someone surely has a script that does this...

Thanks in Advance
# 2  
Old 10-17-2011
Quote:
Originally Posted by Astrocloud
Hey everyone.

What would be the best method to insert commas (or comma-quotes) into a file at set locations.

Every line in the file looks like this:
Code:
 1234567890123456 123456BIGAUDIODYNAMITE33      123.12  123456  12345678901234MARK E WILLIAMS           123456

The comma's should appear at: 11,17,18,20,22,24,40,45,54,62,78,101,103,105,107

Hence the line above should look like (with comma quotes)

Code:
" 1234567890","123456"," ","12","34","56","BIGAUDIODYNAMITE","33   ","   123.12", "  123456","  12345678901234","MARK E WILLIAMS           ","12","34","56

Someone surely has a script that does this...

Thanks in Advance

#!/bin/sh

while read line
do
F1=`echo $line | cut -c1-7`
F2=`echo $line | cut -c8-17`
F3=`echo $line | cut -c17-27`
echo "$F1,$F2,$F3"
done
# 3  
Old 10-17-2011
The script seems to be ignoring multiple spaces in the input.

Adapting to your script I get:

Code:
#!/bin/sh
set -x
while read line
do
F1=`echo $line | cut -c1-10`
F2=`echo $line | cut -c11-16`
F3=`echo $line | cut -c17-17`
F4=`echo $line | cut -c18-19`
F5=`echo $line | cut -c20-21`
F6=`echo $line | cut -c22-23`
F7=`echo $line | cut -c24-39`
F8=`echo $line | cut -c40-44`
F9=`echo $line | cut -c45-53`
F10=`echo $line | cut -c54-61`
F11=`echo $line | cut -c62-77`
F12=`echo $line | cut -c78-100`
F13=`echo $line | cut -c101-102`
F14=`echo $line | cut -c103-104`
F15=`echo $line | cut -c105-107`
echo "$F1,$F2,$F3,$F4,$F5,$F6,$F7,$F8,$F9,$F10,$F11,$F12,$F13,$F14,$F15"
done

root@astrocloud:# ./test.sh
+ read line
1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c1-10
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ F1=1234567890
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c11-16
+ F2=123456
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c17-17
+ F3=
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c18-19
+ F4=12
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c20-21
+ F5=34
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c22-23
+ F6=56
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c24-39
+ F7=BIGAUDIODYNAMITE
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c40-44
+ F8=33 12
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c45-53
+ F9=3.12 1234
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c54-61
+ F10=56 12345
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c62-77
+ F11=678901234MARK E
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c78-100
+ F12=WILLIAMS 123456
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c101-102
+ F13=
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c103-104
+ F14=
+ echo 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456
+ cut -c105-107
+ F15=
+ echo 1234567890,123456, ,12,34,56,BIGAUDIODYNAMITE,33 12,3.12 1234,56 12345,678901234MARK E ,WILLIAMS 123456,,,
1234567890,123456, ,12,34,56,BIGAUDIODYNAMITE,33 12,3.12 1234,56 12345,678901234MARK E ,WILLIAMS 123456,,,


Starting at F8 (bolded above) it should have read in "33 "

Thanks again for any insight.
# 4  
Old 10-17-2011
Hi,
Please find below perl oneliner that may be of use.
Please not I have not completed every field but this should be easy to complete.

myfile.in contains the following line :-
Code:
1234567890123456 123456BIGAUDIODYNAMITE33      123.12  123456  12345678901234MARK E WILLIAMS           123456

Explanation of code:
(.{10}) assigns the first 10 characters to $1 ie 1234567890
(.{6}) assigns the next 6 character to $2 ie 123456 and so on.
/"$1","$2","$3","$4",/g then prints out the fields and adds the quotes and commas were
required.

Code:
perl -p -i -e 's/^(.{10})(.{6})(.{1})(.{2})/"$1","$2","$3","$4",/g' < myfile.in

Output:

Code:
"1234567890","123456"," ","12",3456BIGAUDIODYNAMITE33      123.12  123456  12345678901234MARK E WILLIAMS           123456

Note I have only completed the first four fields and leave it to you to complete if you so wish to.

Cheers
SRG
This User Gave Thanks to Paragon1970 For This Post:
# 5  
Old 10-18-2011
Yes!!! Perl is awesome!
# 6  
Old 10-18-2011
Glad to be of assistance Astrocloud and thanks for my first thanks :-)
# 7  
Old 10-18-2011
Code:
line=" 1234567890123456 123456BIGAUDIODYNAMITE33      123.12  123456  12345678901234MARK E WILLIAMS           123456
"

echo \"${line:1:10}\",\"${line:11:6}\",\"${line:17:1}\",\"${line:18:2}\"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Removing commas from CSV file

Hi I'm creating a sh script to generate a csv file. The CSV contains the values from a sql table. The content looks this: a,b,c,c2,c3,,,,,,,,,,,d,e I have some code that can separate the fields using the comma as delimiter, but some values actually contain commas, such as... (2 Replies)
Discussion started by: preema
2 Replies

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. UNIX for Dummies Questions & Answers

Edit locations in a file

Hi , I have a file which looks like this source1 LEN predictive 392879 394347 0.955489 + . Name=sa000003.1;ID=sa000003;Alias=sa121751.1; source1 LEN descriptive_1 391082 392878 . . . Parent=sa000003.1;supp_id=.1805.1; ... (3 Replies)
Discussion started by: siya@
3 Replies

4. Shell Programming and Scripting

Remove leading commas in the file

Hi , I have a file with below records 965382,10310858,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 767010,10217614,3,10217616,10217622,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,... (4 Replies)
Discussion started by: vputtas@gmail.c
4 Replies

5. Shell Programming and Scripting

Program to insert Delimiters at fixed locations in a file, Can you please Debug it for me??

Can someone please help?I have a file - fixed.txt----------------------------AABBBBCCCCCCDDDEEFFFFGGGGGGHHHIIJJJJKKKKKKLLL----------------------------To insert delimiters at fixed lengths of 2, 4, 6, 3, I created a file text1.txt as-------------------2463----------------------and trying to execute... (10 Replies)
Discussion started by: jd_mca
10 Replies

6. UNIX for Advanced & Expert Users

Insert Delimiter at fixed locations in a flat file

Hi Can somebody help me with solution for this PLEASE? I have a flat file and need to insert delimiters at fixed positions in all the lines so that I can easily convert into EXCEL with columns defined as per their width. For Example Here is the file { kkjhdhal sdfewss sdtereetyw... (7 Replies)
Discussion started by: jd_mca
7 Replies

7. Shell Programming and Scripting

Sending a file to 24 Server locations parallely

Hi All, I have to send a processed file to 24 different server locations. I feel, if this job can be done parallel in the background - Time will come down. I found the script to FTP the file for a single server location through past Unix posts as below: #!/usr/bin/ksh ftp -v -n... (16 Replies)
Discussion started by: vsmeruga
16 Replies

8. Solaris

file locations...

Hi Guys, There was a post that I saw here a while ago regarding file system layout and what to put where, which I am unable to find now.. A user here posted a man page that list what each partition should have in it and what it is used for and were you should install custom packages. ie:... (1 Reply)
Discussion started by: Tornado
1 Replies

9. UNIX for Dummies Questions & Answers

Inserting commas and replacing backslashes with commas

Hi, Newbie here. I have a file that consists of data that I want to convert to a csv file. For example: Jul 20 2008 1111 / visit home / BlackBerry8830/4.2.2 Profile/MIDP-2.0 Configuration/CLOC-1.1 VendorID/105 Jul 21 2008 22222 / add friend / BlackBerry8830/4.2.2 Profile/MIDP-2.0... (3 Replies)
Discussion started by: kangaroo
3 Replies
Login or Register to Ask a Question