insert comma in a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers insert comma in a text file
# 1  
Old 12-15-2010
insert comma in a text file

Hi all,
I have a text file and I need to insert comma after every 2 digit.
Code:
-1-1-1-1-1-1-1-1-1 0 0 0
-1-1-1 2 0 0 3 311-1 0 1
-1-1 021 0 011-1-1 033 0

I'd like to have this:
Code:
-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0
-1,-1,-1, 2, 0, 0, 3, 3,11,-1, 0, 1
-1,-1, 0,21, 0, 0,11,-1,-1, 0,33, 0

Thanks for your help in advance.
# 2  
Old 12-15-2010
Could you please explain why: 0,21, and not 021,?
# 3  
Old 12-15-2010
Quote:
Originally Posted by radoulov
Could you please explain why: 0,21, and not 021,?
These numbers are actually codes and in this file two characters are reserved for each code which vary between -1 and 99.
# 4  
Old 12-15-2010
I don't get the logic ...
# 5  
Old 12-15-2010
Code:
sed 's/../&,/g' myFile

This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 12-15-2010
Quote:
Originally Posted by radoulov
I don't get the logic ...
I'm sorry but my problem is how to add the commas!
# 7  
Old 12-15-2010
Quote:
Originally Posted by vgersh99
Code:
sed 's/../&,/g' myFile

Now I see Smilie

To the OP: you mean every two characters, not every two digits ...

---------- Post updated at 05:17 PM ---------- Previous update was at 05:10 PM ----------

And if you want to get rid of the trailing comma and to edit the file in-place:

Code:
perl -i.bck -ple's/..(?!$)/$&,/g' infile

This User Gave Thanks to radoulov For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

insert comma

my file looks like this: 297 PC Closed 07/10/12 999000098 AMERICAN SOCIETY FOR HEALTHCAR 0.00 USD 1 NAI i want to look line this: 297,PC,Closed,07/10/12,999000098,AMERICAN SOCIETY FOR HEALTHCAR,0.00,USD,1,NAI (4 Replies)
Discussion started by: lawsongeek
4 Replies

3. Shell Programming and Scripting

Append the text file with comma at the end of every word

Hi folks, Using shell, I am trying the append comma to every line of text. the requirement is like, I have to open the txt file in unix and read line by line and should add comma at the end of every word to make it single line txt file ------- abc@gmail.com bcd@gmail.com... (7 Replies)
Discussion started by: giridhar276
7 Replies

4. Shell Programming and Scripting

How to format file into comma separated text file?

Hi Guys, I have text file which is tab/space separated but I want it to re-format into a comma separated and trim the spaces in between. Can someone spare me a perl or sed script that can do the job? INPUT FILE: 500010245623 500 21-APR-11 05.58.21 PM ... (14 Replies)
Discussion started by: pinpe
14 Replies

5. Shell Programming and Scripting

Convert comma text file to Column in html format

I am trying to generate a report with below file : File1 : EQADM,edrtere9-phys,8122caef0,gpatmon,/bin/ksh,nuten Erick EQADM,edrtere11-phys,8227caef0,gpatmon,/bin/ksh,nuten Erick EQADM,edrtere3-phys,822caef0,gpatmon,/bin/ksh,nuten Erick can you help me convert it to html and add... (9 Replies)
Discussion started by: sriram003
9 Replies

6. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

7. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

8. Shell Programming and Scripting

How to replace all entries of comma in text file by space or other character

Hi , How to replace all entries of comma in text file by space or other character. cat temp.txt A,B,C,D I want this file to be like A B C D Please help!!! (4 Replies)
Discussion started by: prashant43
4 Replies

9. Shell Programming and Scripting

How do you delete multiple text from a comma delimited file

I would like to know code that will delete multiple text from a comma delimited file. For example, how would the comma delimited file below delete the word 'PEST' in Perl language (previously an excel file that was converted to a csv and the last column was PEST): 1, 2,43,34, bosx,PEST 1,... (1 Reply)
Discussion started by: dolo21taf
1 Replies

10. Shell Programming and Scripting

Parsing comma delimited text file

I need to delete a set of files in certain directories if there're older than a certain number of days. So I have a text file, with each line containing the directory & number of days. The format is like this: dirA,5 dirB,7 How do I write script to iteratively parse this text file & delete... (5 Replies)
Discussion started by: chengwei
5 Replies
Login or Register to Ask a Question