Insert Commas at fixed positions


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Insert Commas at fixed positions
# 1  
Old 01-27-2010
Insert Commas at fixed positions

Hi

I have a text file which is position delimited, i.e.

Code:
Code1  Description1        Value1
Code2  Description2        Value2 etc.

I want to import this file into MySQL so I want to convert the file into Comma delimited format.

To do this I want to insert commas at fixed positions on each line, eg Position 6 and 26 to give

Code:
Code1,  Description1        ,Value1
Code2,  Description2        ,Value2 etc.

I guess this must be possible with one of the Unix commands but don't know how ! Can anybody help with this ?

Thanks
Ross
# 2  
Old 01-27-2010
You can try this:

Code:
cat testfil2.txt | sed -e 's/Code[0-9]*/&,/g' | sed -e 's/Value[0-9]*/,&/g'


Last edited by Franklin52; 01-27-2010 at 08:46 AM.. Reason: Please use code tags!
# 3  
Old 01-27-2010
Thanks for the quick reply jostber and suggesting sed - however I think I did not make my problem clear enough - the lines of the textfile can contain any text, i.e.

Code:
H1234      Widget type A             12345
ZZ8888     Flanging Sprocket          999

and I need

Code:
H1234      ,Widget type A             ,12345
ZZ8888     ,Flanging Sprocket         ,999

I'll take a look at the docs for sed Smilie

Ross
# 4  
Old 01-27-2010
Quote:
Originally Posted by arossco
To do this I want to insert commas at fixed positions on each line, eg Position 6 and 26
Code:
LINE_OUT=${LINE_IN:0:5}","${LINE_IN:6:20}","${LINE_IN:27:$((${#LINE_IN}-26))}

Code:
Code1  Description1        Value1       # LINE_IN
Code1, Description1       ,Value1       # LINE_OUT

(Reference)
# 5  
Old 01-27-2010
cat testfil2.txt | awk '{print $1, ","$2, ","$3}'
# 6  
Old 01-27-2010
A couple of cheap ones...

Code:
awk '$1=$1",", $NF=","$NF'   # assumes only 3 fields
sed "s/[^ ]  /&,/g"          # assumes at least two spaces between each field

# 7  
Old 01-27-2010
Try something like this:
Code:
sed 's/\(.\{11\}\)\(.\{26\}\)/\1,\2,/'

or
Code:
sed -r 's/(.{11})(.{26})/\1,\2,/'


Last edited by Scrutinizer; 01-27-2010 at 11:05 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert commas

Original content: ACACCTCAT 129 | ACACCTCAT 0 ACACCTCATX 171 | ACACCTCATX 0 ACACRESRT 0 ACACRESRT 0 ACACRESRTX 0 ... (4 Replies)
Discussion started by: loktamann
4 Replies

2. Shell Programming and Scripting

Replace characters at fixed positions

My objective is to replace the 8th, 9th, 10th characters by 1 space per character (total 3 spaces) in a file. I achieved this using following command: sed 's/\(.\)/\1@/7;s/@\(...\)/ /' FileData.txt > FileData_UPDATED.txt Another situation comes when I need to done same but excluding 1st... (5 Replies)
Discussion started by: manishdivs
5 Replies

3. UNIX for Dummies Questions & Answers

Insert single quote and commas

Hi All, I have a set of data as below : XS012371378 Raj 23-09-12 SH128238948 Andrew 24-08-12 CH273712399 Walsh 12-10-12 JK7249923893 Nick 10-02-13 JP6383791389 Braslin 30-12-13 I want the first column to be extracted separately. I can get this using awk. awk '{print $1}' file_name ... (3 Replies)
Discussion started by: Nand Kishor
3 Replies

4. Shell Programming and Scripting

Insert Inverted Commas Around Numeric Values

Hi, I am trying to insert Inverted Commas around all the numeric values within a comma seperated string / variable. 1111,2222,3333,4444 I would like it to be: '1111','2222','3333','4444' Note - This string could have a differing amount of numeric values each time the variable is... (4 Replies)
Discussion started by: RichZR
4 Replies

5. Shell Programming and Scripting

Insert text with Sed (in various positions)

Hello. I'm trying to insert text in various positions and I could only do that using pipes for each position. Example: cat file | sed -e 's#\(.\{5\}\)\(.*\)#\1:\2#g' | sed -e 's#\(.\{26\}\)\(.*\)#\1:\2#g' Insert ":" at position 5 and 26. it can be done in the same sentence, without using... (4 Replies)
Discussion started by: </kida>
4 Replies

6. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

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

8. Shell Programming and Scripting

How to insert a record in fixed width flatfile

I have a fixed width flatfile with 5 columns, i will load file from oracle database to the flatfile for every 15 min at the end of the file, i want to insert a record by calling a shell script for inserting a record. please can any one help me. Thanks. (1 Reply)
Discussion started by: limou
1 Replies

9. UNIX for Dummies Questions & Answers

How do I insert commas/delimiters in File

Hi, Newbie here. Need to convert a txt file to .csv format. There's no character to replace so not sure if I can use sed :confused: . The comma is to be inserted after every certain number of characters in each line... Help! Thanks. (4 Replies)
Discussion started by: mbelen
4 Replies
Login or Register to Ask a Question