Sponsored Content
Top Forums Shell Programming and Scripting Shell script to put delimiter for a no delimiter variable length text file Post 302762447 by Gaurav Martha on Monday 28th of January 2013 12:54:57 PM
Old 01-28-2013
Quote:
Originally Posted by bipinajith
I don't understand. If remaining columns are variable length, then what criteria you will use to separate the data?

BTW If the columns are fixed length then you can define field lengths in a file or variable and perform the separation.
Bipin,

By Variable length I mean.. Like in previous example I taken three number, so some one may have three different phone number and someone may have none...
Code:
sunilbassi031012345678901234567890123456789 
sunilbassi03101234567890123456789 
sunilbassi0310123456789

I have Max length defined for all columns and individual column lengths as well.
Now I want to separate them through delimiter.

Furthermore, If spaces can be added to this variable length file and convert this into fixed length comma separated file. eg:
Code:
sunil,bassi,031,0123456789,0123456789,0123456789 
sunil,bassi,031,0123456789,0123456789,           
sunil,bassi,031,0123456789,          ,

---------- Post updated at 12:54 PM ---------- Previous update was at 12:53 PM ----------

Quote:
Originally Posted by rdrtx1
try also:
Code:
awk '
NR==FNR {if (NF==2) field[++fc]=$2 ; next}
{ el=$0; cc=1; for (i=1; i<=fc; i++) {$i=substr(el,cc,field[i]); cc+=field[i]}}
1
' OFS=, schema_file data_file

Could you please care to explain.

Last edited by Scott; 01-28-2013 at 03:04 PM.. Reason: Code tags...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting a text file based on newline and delimiter characters

Hi Everybody, I need some help on formatting the files coming into unix box on the fly. I get a file some thing like this in a single line. ISA^M00^M ^M00^M ^M14^M006929681900 ^M01^M095449419 ... (5 Replies)
Discussion started by: ntekupal
5 Replies

2. Shell Programming and Scripting

Pivot variable record length file and change delimiter

Hi experts. I got a file (500mb max) and need to pivot it (loading into ORCL) and change BLANK delimiter to PIPE |. Sometimes there are multipel BLANKS (as a particular value may be BLANK, or simply two BLANKS instead of one BLANK). thanks for your input! Cheers, Layout... (3 Replies)
Discussion started by: thomasr
3 Replies

3. UNIX for Dummies Questions & Answers

extract fields from text file using delimiter!!

Hi All, I am new to unix scripting, please help me in solving this assignment.. I have a scenario, as follows: 1. i have a text file(read1.txt) with the following data sairam,123 kamal,122 etc.. 2. I have to write a unix... (6 Replies)
Discussion started by: G.K.K
6 Replies

4. Shell Programming and Scripting

Adding a delimiter to a text file

Im writing a KSH script to read a simple text file and add a delimiter. Ive written the following script but it runs very slow. I initially used the cut command to substring the input record then switched to this version using awk to substring... both run too slow. Any ideas how to make this more... (2 Replies)
Discussion started by: lock
2 Replies

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

6. Shell Programming and Scripting

Adding a delimiter to a variable length file

Hi, I'm new to unix, i have a variable length file like below, 01|Test|Test1|Sample| 02|AA|BB|CC|DD| 03|AAA|BBB|CCC|DDD|EEE|RRR|TTT|SSS|YYY| I need to make this as a fixed length file. Assume that i have 10 columns in the DAT file. for ex: the first 01 record is having 4cols -... (8 Replies)
Discussion started by: Mohankumar Venu
8 Replies

7. Shell Programming and Scripting

Splitting records in a text file based on delimiter

A text file has 2 fields (Data, Filename) delimited by # as below, Data,Filename Row1 -> abc#Test1.xml Row2 -> xyz#Test2.xml Row3 -> ghi#Test3.xml The content in first field has to be written into a file where filename should be considered from second field. So from... (4 Replies)
Discussion started by: jayakkannan
4 Replies

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

9. Shell Programming and Scripting

How to target certain delimiter to split text file?

Hi, all. I have an input file. I would like to generate 3 types of output files. Input: LG10_PM_map_19_LEnd_1000560 LG10_PM_map_6-1_27101856 LG10_PM_map_71_REnd_20597718 LG12_PM_map_5_chr_118419232 LG13_PM_map_121_24341052 LG14_PM_1a_456799 LG1_MM_scf_5a_opt_abc_9029993 ... (5 Replies)
Discussion started by: huiyee1
5 Replies

10. UNIX for Beginners Questions & Answers

Shell script to Split matrix file with delimiter into multiple files

I have a large semicolon delimited file with thousands of columns and many thousands of line. It looks like: ID1;ID2;ID3;ID4;A_1;B_1;C_1;A_2;B_2;C_2;A_3;B_3;C_3 AA;ax;ay;az;01;02;03;04;05;06;07;08;09 BB;bx;by;bz;03;05;33;44;15;26;27;08;09 I want to split this table in to multiple files: ... (1 Reply)
Discussion started by: trymega
1 Replies
GFS_PIO_READDELIM(3)													      GFS_PIO_READDELIM(3)

NAME
gfs_pio_readdelim - read one record SYNOPSIS
#include <gfarm/gfarm.h> char *gfs_pio_readdelim (GFS_File f, char **bufp, size_t *sizep, size_t *lenp, char *delimiter, size_t delimlen); DESCRIPTION
gfs_pio_readdelim() works like gfs_pio_readline(), except a delimiter of input records is not always newline, and can be specified. This function reads one record from the file specified by the parameter gf, by using the parameter delimiter as the delimiter of the input records. You can include '' character in the delimiter, So, you have to specify the length of the delimiter by the parameter delimlen. If parameter delimiter is NULL, this function reads entire file as one record. Otherwise, and if the parameter delimlen is 0, this func- tion treats two or more consecutive empty lines (/ +/ in a regular expression) as the input delimiter. This feature is derived from INPUT_RECORD_SEPARATOR in perl language. Parameter bufp specifies an address of a pointer variable initialzed by NULL at first. gfs_pio_readdelim() allocates a buffer for I/O dynamically, and stores the address of the buffer to this variable pointed by bufp. Parameter sizep specifies an address of a size_t vari- able initialized by 0. This size_t variable is used to record the size of the buffer. Or, you can specify a buffer allocated by malloc(3) in the variable pointed by the parameter bufp. In this case, you have to specify the size of the allocated buffer by the parameter sizep. If the length of the record exceeds the size of the buffer, the buffer will be automatically realloc(3)ed, and the variable pointed by bufp and sizep will be updated respectively. Note that you are responsible to free(3) this buffer. This function returns the length of the record to a variable pointed by the parameter lenp. This length includes the length of the record delimiter. This function doesn't remove the delimiter at the end of records. Also, despite that you can use the value returned by the variable pointed by lenp, this function always appends ' character at the end of records. If the file reaches its end, the length of the result record becomes 0. gfs_pio_readdelim(f, bufp, sizep, lenp, " ", 1) is equivalent to gfs_pio_readline() function. RETURN VALUES
NULL The function terminated successfully. GFARM_ERR_NO_MEMORY Insufficient memory was available. Note that you need to free(3) the buffer pointed by the parameter bufp Others An error except the above occurred. The reason is shown by its pointed strings. EXAMPLES
EXAMPLE OF GFS_PIO_READDELIM FUNCTION #include <stdio.h> #include <stdlib.h> #include <string.h> #include <gfarm/gfarm.h> int main(int argc, char **argv) { char *e; GFS_File gf; size_t bufsize = 0, delimlen = 1, len; char *buffer = NULL, *delim = " "; e = gfarm_initialize(&argc, &argv); if (e != NULL) { fprintf(stderr, "gfarm_initialize: %s ", e); return (EXIT_FAILURE); } while ((c = getopt(argc, argv, "d:D")) != -1) { switch (c) { case 'd': delim = optarg; delimlen = strlen(optarg); break; case 'D': delim = NULL; delimlen = 0; break; case '?': default: fprintf(stderr, "invalid option: %c ", c); return (EXIT_FAILURE); } } if (optind >= argc) { fprintf(stderr, "missing gfarm filename "); return (EXIT_FAILURE); } e = gfs_pio_open(argv[optind], GFARM_FILE_RDONLY, &gf); if (e != NULL) { fprintf(stderr, "%s: %s ", argv[optind], e); return (EXIT_FAILURE); } e = gfs_pio_set_view_global(gf, 0); if (e != NULL) { fprintf(stderr, "%s: gfs_pio_set_view_global: %s ", argv[optind], e); return (EXIT_FAILURE); } while ((e = gfs_pio_readdelim(gf, &buffer, &bufsize, &len, delim, delimlen)) == NULL && len > 0) { printf("<%6d/%6d >%s", len, bufsize, buffer); } if (buffer != NULL) free(buffer); if (e != NULL) { fprintf(stderr, "ERROR: %s ", e); return (EXIT_FAILURE); } e = gfs_pio_close(gf); if (e != NULL) { fprintf(stderr, "gfs_pio_close: %s ", e); return (EXIT_FAILURE); } e = gfarm_terminate(); if (e != NULL) { fprintf(stderr, "gfarm_initialize: %s ", e); return (EXIT_FAILURE); } return (EXIT_SUCCESS); } SEE ALSO
gfs_pio_open(3), gfs_pio_getline(3), gfs_pio_gets(3), gfs_pio_readline(3) Gfarm 13 May 2004 GFS_PIO_READDELIM(3)
All times are GMT -4. The time now is 04:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy