If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Separating the Columns based on the ColumnSize

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-28-09, 08:20
SeenuGuddu SeenuGuddu is offline
Registered User
 
Join Date: Sep 2009
Posts: 16
Separating the Columns based on the ColumnSize

i had Client Table with the colummns
Code:
col1 		 Decimal(3),
col2 		 Decimal(2),
col3 		 String(1),
col4_blank1* string(2),
col5_blank2 *string(10),
col6	     string(3);


the flat file is

89522D            085NOVOLIN BRAND                 888888ALL OTHER

i want to dispaly the output by seperating the columns with ~ based on the column size is 3 like col1 Decimal(3) and col2 having the size 2

895~22~D~    ~          ~085~NOVOLIN
Thanks in Advance

Last edited by SeenuGuddu; 10-28-09 at 10:44.
Reply With Quote
  #2 (permalink)  
Old 10-28-09, 08:56
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
This starts you off. It passes your file through a small awk program. You'll need to expand the awk program to do all your fields - alter the 2nd param to be the postion in the line and the 3rd param to be the number of characters to use. In your code the 3rd field is 1 character but your example seems to hold many characters. You might also be able to use the cut with the -c option to get certain parts and then perhaps use the output-delimiter option to put in the ~.
Code:
#!/bin/sh

cat your_file | \
awk '{ print    substr( $0,1,3 ) "~" substr( $0,4,2 ) "~" substr( $0,6,1 ); }'

exit
Reply With Quote
  #3 (permalink)  
Old 10-29-09, 06:06
SeenuGuddu SeenuGuddu is offline
Registered User
 
Join Date: Sep 2009
Posts: 16
in Temp table having the column size Value

Code:
Id Column_name     Col_size 
1    col1                   3
2    col2                   2
3    col3                   1

the records of the file  is 

89522D            085

based on the column size.. i have to delimitate the records of file into 

895~22~D~            085
can u give some example on this

Last edited by SeenuGuddu; 10-29-09 at 06:11.
Reply With Quote
  #4 (permalink)  
Old 10-29-09, 06:52
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by SeenuGuddu
can u give some example on this
This example just runs your example data:
Code:
#!/bin/sh
echo "89522D            085NOVOLIN BRAND                 888888ALL OTHER" | \
awk '{ print    substr( $0,1,3 ) "~" substr( $0,4,2 ) "~" substr( $0,6,1 ); }'

exit
Running the above produces:
Code:
895~22~D
Reply With Quote
  #5 (permalink)  
Old 10-29-09, 07:37
SeenuGuddu SeenuGuddu is offline
Registered User
 
Join Date: Sep 2009
Posts: 16
Appending ~ delimeters between column values.

Hi,

Thanks for your reply.

i had 100 records in each record having 50 columns of Data
Code:
sams11Y
johns12Y
ravi13N

 how would i get the position of the start string in substr( $0,1,3 )

in that loop of each record i have to print with ~ delimated  like 
sams~11~Y
johns~12~Y
Thanks

Last edited by SeenuGuddu; 10-29-09 at 07:56.
Reply With Quote
  #6 (permalink)  
Old 10-29-09, 08:24
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by SeenuGuddu
how would i get the position of the start string in substr( $0,1,3 )
I'd suggest counting. The first character in the line is 1, the next is 2 and so fourth. You then put this value in the 2nd field above. The length of the field goes into the 3rd field. Repeat. This measuring device might also help:
Code:
         1         2         3         4         5         6
123456789012345678901234567890123456789012345678901234567890
89522D            085NOVOLIN BRAND                 888888ALL OTHER
895 starts at position 1.
22 starts at position 4.
D starts at position 6.
...

Mike
Reply With Quote
  #7 (permalink)  
Old 10-29-09, 08:41
SeenuGuddu SeenuGuddu is offline
Registered User
 
Join Date: Sep 2009
Posts: 16
Thanks Mike

Can u share the Example for this start String and End String in substr( $0,1,3 )
Reply With Quote
  #8 (permalink)  
Old 10-29-09, 09:59
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
it generally doesn't hurt if you try a little experimenting for yourself ...
Reply With Quote
  #9 (permalink)  
Old 10-30-09, 09:15
SeenuGuddu SeenuGuddu is offline
Registered User
 
Join Date: Sep 2009
Posts: 16
sure Mike .. i will get back to u.

Thanks
Reply With Quote
  #10 (permalink)  
Old 11-20-09, 09:37
SeenuGuddu SeenuGuddu is offline
Registered User
 
Join Date: Sep 2009
Posts: 16
hi mike

here is the code but i have some problem with that

${COL_SIZE_DAT its a flat file of records of column size

Code:
	normalfile="john_789_has_2007-11-30_s"
	
    j=1
    export delim=""
    while read col_size_dat
    do 
        export col_size=$(echo "${col_size_dat}" | cut -f2 -d"|")
        export delim_temp="${delim} substr( $0,$j,$col_size) \"~\"" 
        j=`expr $j + ${col_size}`   
    done < ${COL_SIZE_DAT}
   
   export delim_temp1=$(echo "$delim_temp" | sed 's/\(.*\)"~"/\1/')   
   export delim_cmd=`echo " awk '{ print ${delim_temp1}; }' " ` 
   echo $delim_cmd
 
  cat normalfilename | $delim_cmd > delimatedfile
instead of normalfile="john_789_has_2007-11-30_s"
u want me to give normalfile for $0 or any other way we had the substr( $0,$j,$col_size)

wat should i use for redirecting the filewith delimated
is that ok

cat normalfilename | $delim_cmd > delimatedfile
and after running this
i am getting the error

awk: cmd. line:1: '{
awk: cmd. line:1: ^ invalid char ''' in expression

Last edited by SeenuGuddu; 11-23-09 at 00:32.
Reply With Quote
  #11 (permalink)  
Old 11-22-09, 23:08
AnanthaP AnanthaP is offline
Registered User
 
Join Date: May 2009
Location: India
Posts: 62
col1 Decimal(3), cols 1,2,3 substr($0,1,3) in awk
col2 Decimal(2), cols 4,5 : substr($0,4,2) in awk
col3 String(1), and so on
col4_blank1* string(2),
col5_blank2 *string(10),
col6 string(3);

as another post said, it helps if you try it yourself first.

End
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On