How do I insert commas/delimiters in File


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do I insert commas/delimiters in File
# 1  
Old 05-19-2005
Question 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 Smilie . The comma is to be inserted after every certain number of characters in each line...

Help!

Thanks.
# 2  
Old 05-19-2005
Code:
echo '123456789' | /usr/xpg4/bin/sed 's/.\{2\}/&,/g'

# 3  
Old 05-19-2005
Thanks, but...

Sorry for being a pain here, but I can't make it work. Let's say I have file test1 which contains the following :

hellothereput1here
hellother2put2here

and I want to put the comma after the 5th 10th 13th 14th 18th char of every line. So my output should be :

hello,there,put,1,here,
hello,ther2,put,2,here,

Appreciate all the help!
# 4  
Old 05-19-2005
Quote:
Originally Posted by mbelen
Sorry for being a pain here, but I can't make it work. Let's say I have file test1 which contains the following :

hellothereput1here
hellother2put2here

and I want to put the comma after the 5th 10th 13th 14th 18th char of every line. So my output should be :

hello,there,put,1,here,
hello,ther2,put,2,here,

Appreciate all the help!
the above was just an example.

here's something to chew on:
Code:
echo 'hellothereput1here' | /usr/xpg4/bin/sed 's/^\(.\{5\}\)\(.\{5\}\)\(.\{3\}\)\(.\)\(.\{4\}\)/\1,\2,\3,\4,\5,/g'

or a little bit easier with gawk:
Code:
echo 'hellothereput1here' | gawk -v FIELDWIDTHS='5 5 3 1 4' -v OFS=',' '$1=$1'


Last edited by vgersh99; 05-19-2005 at 07:09 PM..
# 5  
Old 05-19-2005
MySQL Thank You very much

Thank you for spelling it out for me. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Insert a hyphen between two delimiters using sed

Hey guys, I have a file that is delimited by | and I am trying to write a sed command to convert this: abc|def||ghi|jkl||||mnop into this: abc|def|-|ghi|jkl|-|-|-|mnop The output I am getting out of: sed -e "s/+//g" /tmp/opt.del > /tmp/opt2.del is like: ... (9 Replies)
Discussion started by: prohank
9 Replies

3. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

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

5. Shell Programming and Scripting

Insert data between comma delimiters-large file

Having a huge file in the following format. 2,3,1,,,4 1,2,3,,,,,5, 8,7,3,4,,,, Output needed is: 2,3,1,0.0,0.0,4 1,2,3,0.0,0.0,0.0,0.0,5, 8,7,3,4,0.0,0.0,0.0, I have tried reading the file each line, using AWK to parse to find out ",," and then insert 0.0 . It works but very slow. Need... (8 Replies)
Discussion started by: wincrazy
8 Replies

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

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

8. UNIX for Dummies Questions & Answers

Insert Commas at fixed positions

Hi I have a text file which is position delimited, i.e. 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... (7 Replies)
Discussion started by: arossco
7 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

10. Shell Programming and Scripting

Insert lines between delimiters

I'm working with a file like: somestuff somemorestuff ... someadditionalstuff STARTTAG ENDTAG someotherstuff somecoolstuff ... somefinalstuffI've got some text (either in a file or piped) to put between STARTTAG and ENDTAG. I was thinking something like grepping for the line number of... (2 Replies)
Discussion started by: BMDan
2 Replies
Login or Register to Ask a Question