insert delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert delimiter
# 1  
Old 06-24-2011
insert delimiter

I have a data file that I would like to add delimiters to.
Example:

Turn This:
20110624000744000693000704000764

Into This:
20110624,000744,000693,000704,000764

I found this link but the only issue is I do not know how many colums I will have. The first field would needs to be 8 characters, but then everything after that needs to be 6 characters. There is somewhere between 200-300 characters per line.
https://www.unix.com/unix-advanced-ex...flat-file.html

Any one know a command to do this?

Last edited by oldman2; 06-24-2011 at 06:37 PM..
# 2  
Old 06-24-2011
What's your system? What's your shell? I don't know an all-in-one command to do this, not surprising since that'd be a bit specialized, but a program to do so is doable:
Code:
#!/bin/bash

while read LINE
do
        STR="${LINE:0:8}"
        REST="${LINE:8}"

        while [ "${#REST}" -gt 0 ]
        do
                STR="${STR},${REST:0:6}"
                REST="${REST:6}"
        done

        echo "$STR"
done

Older shells may not have this substring syntax.
# 3  
Old 06-24-2011
I got a bit close with this:

Code:
$ sed "s/.\{8\}/&,/;s/[^\,]\{6\}/&,/g;s/,//" file1
20110624,000744,000693,000704,000764,

That's a garbled command - if ever I saw one! - even if SED didn't report it this time Smilie

I suppose, "garbling" a bit more, adding ;s/,$// at the end would fix it!
Code:
20110624,000744,000693,000704,000764

This User Gave Thanks to Scott For This Post:
# 4  
Old 06-24-2011
Here's an awk version that'll work more places than my bash one.

Code:
$ echo 20110624000744000693000704000764 | 
        awk '{
        S=substr($0,1,8);

        for(N=9; N<=length($0); N+=6)
                S=S "," substr($0, N, 6);

        print STR; }'

20110624,000744,000693,000704,000764
$

# 5  
Old 06-24-2011
alternate awk

Code:
echo 20110624000744000693000704000764 | awk -v FS="" -v OFS="" '
{ for (i = 9; $i ~ /[0-9]/; i += 6) $i=","$i; } 1'

20110624,000744,000693,000704,000764

# 6  
Old 06-24-2011
[edit] Figured it out! You're not splitting it in chunks, you're adding , in front of select characters. Neat.
# 7  
Old 06-24-2011
Code:
echo 20110624000744000693000704000764 |  
perl -lne '
($pref, $suf) = /(.{8})(.*)/;
$suf =~ s/.{6}(?=.)/$&./g;
print "$pref.$suf"'
20110624.000744.000693.000704.000764

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a value between two empty delimiter in the file.

Would like to insert value between two empty delimiter and at the very last too if empty. $ cat customerleft.tbl 300|Customer#000000300|I0fJfo60DRqQ|7|17-165-193-5964|8084.92|\N|p fluffily among the slyly express grouches. furiously express instruct||||||||||||||||||||||||\N... (3 Replies)
Discussion started by: Mannu2525
3 Replies

2. Shell Programming and Scripting

Linux shell script to insert new lines based on delimiter count

The input file is a .dat file which is delimited by null (^@ in Linux). On a windows PC it looks something like this (numbers are masked with 1). https://i.imgur.com/nta2Gqp.jpg The entire file is in one row but it has multiple records - each record contains 80 fields i.e. there are 81 counts... (9 Replies)
Discussion started by: digitalnirvana
9 Replies

3. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

4. Shell Programming and Scripting

[Solved] Insert tabs as delimiter

Hello all, I have an unstructured file with space as delimiter , which I want to structure. The output file should actually have only 5 columns with tab as delimiter. The 4th column can have only 3 values ( biological_process , cellular_component , molecular_function ) Here is how the... (12 Replies)
Discussion started by: ritakadm
12 Replies

5. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

6. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

7. Shell Programming and Scripting

Selecting Specific Columns and Insert the delimiter TAB

Hi, I am writing a Perl Script for the below : I have a data file that consists of the header information which is 231 Lines and the footer information as 4 lines. The total number of line including the header and footer 1.2 Million with Pipe Delimited file. For example: Header Information:... (4 Replies)
Discussion started by: filter
4 Replies

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

9. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

10. Shell Programming and Scripting

Insert text between delimiter

Can someone help me on this? I'm creating an Insert stmt script but Oracle does not accept blanks values. How can I insert the word null between two commas? I'm guessing awk or sed. Is there a good post or site with easy to understand info on awk and sed? I'm really new to unix scripts :D ... (5 Replies)
Discussion started by: ystee
5 Replies
Login or Register to Ask a Question