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
BASENAME(3)								 1							       BASENAME(3)

basename - Returns trailing name component of path

SYNOPSIS
string basename (string $path, [string $suffix]) DESCRIPTION
Given a string containing the path to a file or directory, this function will return the trailing name component. PARAMETERS
o $path - A path. On Windows, both slash ( /) and backslash ( ) are used as directory separator character. In other environments, it is the forward slash ( /). o $suffix - If the name component ends in $suffix this will also be cut off. RETURN VALUES
Returns the base name of the given $path. EXAMPLES
Example #1 basename(3) example <?php echo "1) ".basename("/etc/sudoers.d", ".d").PHP_EOL; echo "2) ".basename("/etc/sudoers.d").PHP_EOL; echo "3) ".basename("/etc/passwd").PHP_EOL; echo "4) ".basename("/etc/").PHP_EOL; echo "5) ".basename(".").PHP_EOL; echo "6) ".basename("/"); ?> The above example will output: 1) sudoers 2) sudoers.d 3) passwd 4) etc 5) . 6) NOTES
Note basename(3) operates naively on the input string, and is not aware of the actual filesystem, or path components such as " ..". Note basename(3) is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale(3) function. SEE ALSO
dirname(3), pathinfo(3). PHP Documentation Group BASENAME(3)
All times are GMT -4. The time now is 03:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy