Insert commas


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Insert commas
# 1  
Old 10-08-2016
Insert commas

Code:
Original content:
ACACCTCAT    129                                              | ACACCTCAT    0
ACACCTCATX   171                                              | ACACCTCATX   0
ACACRESRT    0                                                  ACACRESRT    0
ACACRESRTX   0                                                  ACACRESRTX   0
ACACTCOPY    0                                                  ACACTCOPY    0
ACACTGRP     12                                               | ACACTGRP     0
ACACTIVITY   82                                               | ACACTGRPAU   0
ACACTMXVAL   145                                              | ACACTIVITY   0
                                                              > ACACTMXVAL   0
ACACTMXVDT   0                                                  ACACTMXVDT   0
ACACTREL     0                                                  ACACTREL     0
ACACTSEG     0                                                  ACACTSEG     0
ACADDASSGN   0                                                  ACADDASSGN   0
ACADDLCNTR   4                                                | ACADDLCNTR   0
UKCOTABLE    648                                              <
UKNICAT      37                                               <


Result:
ACACCTCAT,    129,                                              | ACACCTCAT,    0
ACACCTCAT,   171,                                              | ACACCTCATX,   0
ACACRESRT,    0,                                                  ACACRESRT,    0
ACACRESRTX,   0,                                                  ACACRESRTX,   0
ACACTCOPY,    0,                                                  ACACTCOPY,    0
ACACTGRP,     12,                                               | ACACTGRP,     0
ACACTIVITY,   82,                                               | ACACTGRPAU,   0
ACACTMXVAL,   145,                                              | ACACTIVITY,   0
                    ,      ,                                    > ACACTMXVAL,   0
ACACTMXVDT,   0,                                                  ACACTMXVDT,   0
ACACTREL,     0,                                                  ACACTREL,     0
ACACTSEG,     0,                                                  ACACTSEG,     0
ACADDASSGN,   0,                                                  ACADDASSGN,   0
ACADDLCNTR,   4,                                                | ACADDLCNTR,   0
UKCOTABLE,    648,                                              <,
UKNICAT,      37,                                               <,

I know a little bit of sed and awk. If you guru can show me either one of the commands, greatly appreciated. I appreciate any help you can provide.
I tried
Code:
sed 's/............./&,/g'

and it insert comma (,) at every 14th position.

Last edited by Scrutinizer; 10-08-2016 at 06:29 PM.. Reason: Changed position of [/code]
# 2  
Old 10-08-2016
Hi,
with gnu sed:
Code:
$ cat /tmp/f2.txt 
ACACCTCAT    129                                              | ACACCTCAT    0
ACACCTCATX   171                                              | ACACCTCATX   0
ACACRESRT    0                                                  ACACRESRT    0
ACACRESRTX   0                                                  ACACRESRTX   0
ACACTCOPY    0                                                  ACACTCOPY    0
ACACTGRP     12                                               | ACACTGRP     0
ACACTIVITY   82                                               | ACACTGRPAU   0
ACACTMXVAL   145                                              | ACACTIVITY   0
                                                              > ACACTMXVAL   0
ACACTMXVDT   0                                                  ACACTMXVDT   0
ACACTREL     0                                                  ACACTREL     0
ACACTSEG     0                                                  ACACTSEG     0
ACADDASSGN   0                                                  ACADDASSGN   0
ACADDLCNTR   4                                                | ACADDLCNTR   0
UKCOTABLE    648                                              <
UKNICAT      37                                               <

Code:
$ sed -e ':bc;/.\{79\}/!s/$/ /;tbc;s/./&,/13;s/./&,/63;s/./&,/79' /tmp/f2.txt 
ACACCTCAT    ,129                                              ,| ACACCTCAT    ,0 
ACACCTCATX   ,171                                              ,| ACACCTCATX   ,0 
ACACRESRT    ,0                                                ,  ACACRESRT    ,0 
ACACRESRTX   ,0                                                ,  ACACRESRTX   ,0 
ACACTCOPY    ,0                                                ,  ACACTCOPY    ,0 
ACACTGRP     ,12                                               ,| ACACTGRP     ,0 
ACACTIVITY   ,82                                               ,| ACACTGRPAU   ,0 
ACACTMXVAL   ,145                                              ,| ACACTIVITY   ,0 
             ,                                                 ,> ACACTMXVAL   ,0 
ACACTMXVDT   ,0                                                ,  ACACTMXVDT   ,0 
ACACTREL     ,0                                                ,  ACACTREL     ,0 
ACACTSEG     ,0                                                ,  ACACTSEG     ,0 
ACADDASSGN   ,0                                                ,  ACADDASSGN   ,0 
ACADDLCNTR   ,4                                                ,| ACADDLCNTR   ,0 
UKCOTABLE    ,648                                              ,<              ,  
UKNICAT      ,37                                               ,<              ,

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 10-08-2016
Quote:
Originally Posted by loktamann
Code:
Original content:
ACACCTCAT    129                                              | ACACCTCAT    0
ACACCTCATX   171                                              | ACACCTCATX   0
ACACRESRT    0                                                  ACACRESRT    0
ACACRESRTX   0                                                  ACACRESRTX   0
ACACTCOPY    0                                                  ACACTCOPY    0
ACACTGRP     12                                               | ACACTGRP     0
ACACTIVITY   82                                               | ACACTGRPAU   0
ACACTMXVAL   145                                              | ACACTIVITY   0
                                                              > ACACTMXVAL   0
ACACTMXVDT   0                                                  ACACTMXVDT   0
ACACTREL     0                                                  ACACTREL     0
ACACTSEG     0                                                  ACACTSEG     0
ACADDASSGN   0                                                  ACADDASSGN   0
ACADDLCNTR   4                                                | ACADDLCNTR   0
UKCOTABLE    648                                              <
UKNICAT      37                                               <


Result:
ACACCTCAT,    129,                                              | ACACCTCAT,    0
ACACCTCAT,   171,                                              | ACACCTCATX,   0
ACACRESRT,    0,                                                  ACACRESRT,    0
ACACRESRTX,   0,                                                  ACACRESRTX,   0
ACACTCOPY,    0,                                                  ACACTCOPY,    0
ACACTGRP,     12,                                               | ACACTGRP,     0
ACACTIVITY,   82,                                               | ACACTGRPAU,   0
ACACTMXVAL,   145,                                              | ACACTIVITY,   0
                    ,      ,                                    > ACACTMXVAL,   0
ACACTMXVDT,   0,                                                  ACACTMXVDT,   0
ACACTREL,     0,                                                  ACACTREL,     0
ACACTSEG,     0,                                                  ACACTSEG,     0
ACADDASSGN,   0,                                                  ACADDASSGN,   0
ACADDLCNTR,   4,                                                | ACADDLCNTR,   0
UKCOTABLE,    648,                                              <,
UKNICAT,      37,                                               <,

I know a little bit of sed and awk. If you guru can show me either one of the commands, greatly appreciated. I appreciate any help you can provide.
I tried
Code:
sed 's/............./&,/g'

and it insert comma (,) at every 14th position.
With problems like this, it is always a good idea to tell us what operating system and shell you're using. And, we need a clearer description of what constitutes a field in your input file and where the commas are to be inserted into your output. (I have no idea why the 1st two commas in the line marked in red above are positioned where they are???)

While it is true that the sed command you showed us adds a comma after every string of 13 characters found on each line of its input, I don't see how that is related to the output you displayed in the result you are trying to achieve.

It sort of looks like you have five fixed-width fields and any two of those fields might be missing from an input line. If all of the fields contain non-blank characters, you want to insert a comma at the end of the non-blank characters in fields 1, 2, and 4 if field 4 is present and after fields 1, 2, and 3 if field 4 is missing. And, if field 5 is present, a space is to be added to the end of field 5. But, as I mentioned before, if fields 1 and 2 do not contain any non-blank characters, I don't understand why the commas on that line are where they are in your desired result???

If you aren't on a system with GNU sed, or if the script disedorgue provided doesn't do what you need, please give us a much clearer specification of what you are trying to do and tell us what operating system and shell you're using.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 10-08-2016
Thank you both so much for helping out. I am on windows with cygwin. It seems to work fine for me. Again, thank you!
# 5  
Old 10-09-2016
This might come close to what you desired (except for the nearly empty line and the last and second last one):
Code:
sed 's/\([^ ]\)\( \)/\1,\2/g' file
ACACCTCAT,    129,                                              |, ACACCTCAT,    0
ACACCTCATX,   171,                                              |, ACACCTCATX,   0
ACACRESRT,    0,                                                  ACACRESRT,    0
ACACRESRTX,   0,                                                  ACACRESRTX,   0
ACACTCOPY,    0,                                                  ACACTCOPY,    0
ACACTGRP,     12,                                               |, ACACTGRP,     0
ACACTIVITY,   82,                                               |, ACACTGRPAU,   0
ACACTMXVAL,   145,                                              |, ACACTIVITY,   0
                                                              >, ACACTMXVAL,   0
ACACTMXVDT,   0,                                                  ACACTMXVDT,   0
ACACTREL,     0,                                                  ACACTREL,     0
ACACTSEG,     0,                                                  ACACTSEG,     0
ACADDASSGN,   0,                                                  ACADDASSGN,   0
ACADDLCNTR,   4,                                                |, ACADDLCNTR,   0
UKCOTABLE,    648,                                              <
UKNICAT,      37,                                               <

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

Replace field with commas with field without commas

Hey guys, I have the following text: 1,2,3,4,5,6,'NULL','when',NULL,1,2,0,'NULL' 1,2,3,4,5,6,'NULL','what','NULL',1,2,0,1 I need the same text with the word NULL without commas u know something like this: 1,2,3,4,5,6,NULL,'when',NULL,1,2,0,NULL 1,2,3,4,5,6,NULL,'what','NULL',1,2,0,1 ... (1 Reply)
Discussion started by: lmyk72
1 Replies

4. Shell Programming and Scripting

Commas within Delimeters

Hi experts, I would like a favour from you guys to get the info from 5th column which was separated by the delimeter comma ( , ) The Data file is as below:- 1,USER1,"90, TEST AVENUE, OLD ROAD",test1,124,N 2,USER2,88 TEST STREET NEW ROAD,test2,123,N The User File is as below:- USER1... (1 Reply)
Discussion started by: shenkz
1 Replies

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

6. Shell Programming and Scripting

Need help in removing commas

i have the below line as output from a script. I want to delete the string "," and get the output without comma, cat D* | grep "bytes free" | awk '{print $3}' | ????? output: 40,966,189,056 Desired O/P: 40966189056 (1 Reply)
Discussion started by: ali560045
1 Replies

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

8. Shell Programming and Scripting

substitute commas with pipe

Hi All, I have a file that contain value below: test,mno,mno, +asc mno,lok,msyu,tts test,poi,test,0,90, 3,00 i need to substitute the comma's into pipe where i... (2 Replies)
Discussion started by: selamba_warrior
2 Replies

9. Shell Programming and Scripting

formatted output with commas

var=12345 echo $var >>>12345 printf "%8.1f \n" $var >>> 12345.0 How to get this as 12,345? I suppose I could break into sections by dividing by 1000 or 1000000. But, is the a trick to this? (4 Replies)
Discussion started by: joeyg
4 Replies

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