Sponsored Content
Top Forums Shell Programming and Scripting Inserting commas into file at set locations... Post 302565352 by Astrocloud on Monday 17th of October 2011 04:02:57 PM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

8. 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

9. 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
py_xls2csv(1)						      General Commands Manual						     py_xls2csv(1)

NAME
py_xls2csv - convert an Excel xls file to a comma separated value csv file SYNOPSIS
py_xls2csv input-file DESCRIPTION
This manual page was written for the Debian distribution because the original program does not have a manual page. py_xls2csv takes an Excel xls file as an argument and converts it to a comma separated value csv file. Output is sent to stdout. Additional utility scripts can be found in the tools/ directory. OPTIONS
This program does not take any command line options. AUTHOR
pyexcelerator and py_xls2csv were written by Roman V. Kiseliov <roman@kiseliov.ru>. This manual page was written by Kevin Coyner <kcoyner@debian.org> for the Debian system (but may be used by others). Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License, Version 2 any later version published by the Free Software Foundation. On Debian systems, the complete text of the GNU General Public License can be found in /usr/share/common-licenses/GPL. SEE-ALSO pyexcelerator(1), py_xls2txt(1), py_xls2html(1) October 12, 2006 py_xls2csv(1)
All times are GMT -4. The time now is 05:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy