The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
combining fields in two text fields shocker Shell Programming and Scripting 3 01-16-2008 11:27 AM
GNU PDF to fill missing gap in functionality iBot UNIX and Linux RSS News 0 12-01-2007 11:05 AM
fill a NIL into the blank field happyv Shell Programming and Scripting 8 03-23-2007 04:49 AM
align several fields and fill spaces with zero DebianJ Shell Programming and Scripting 2 11-23-2005 07:51 AM
Esc key/Auto-fill mr_manny SUN Solaris 3 10-30-2005 07:12 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-30-2008
charandevu charandevu is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 29
Fill the Key fields : Please help us

Hi ....

we are having the below file .Column 1, Column 2 ,column 3 are key fields...
In the below ...for 2 nd , 3 rd row the repeated key column is missing ....

i want the new file to be populated with all missing key columns.


E100,0,5/29/1993,0,E001,E000,A,500000,41666.667,240.384615
_____0,1/23/1994,0,E001,E003,A,125400,10450,60.288462
_____0,6/4/1994,0,E001,E003,A,95000,7916.667,45.673077
E101,0,7/30/1993,0,E001,E003,A,87000,7250,41.826923
______ 1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E103,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E104,0,2/15/1995,0,E001,E001,A,78000,6500,37.5


I want the result to be :


E100,0,5/29/1993,0,E001,E000,A,500000,41666.667,240.384615
E100, 0,1/23/1994,0,E001,E003,A,125400,10450,60.288462
E100, 0,6/4/1994,0,E001,E003,A,95000,7916.667,45.673077
E101,0,7/30/1993,0,E001,E003,A,87000,7250,41.826923
E101,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E103,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E104,0,2/15/1995,0,E001,E001,A,78000,6500,37.5



Please help me ........
  #2 (permalink)  
Old 03-30-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Write an awk script which remembers the fields from the previous line, and adds as many to the front as are missing to form a full line. Assume the first line indicates how many fields a full line should have. Read up on array handling in the awk documentation.
  #3 (permalink)  
Old 03-31-2008
charandevu charandevu is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 29
hi era

Quote:
Originally Posted by era View Post
Write an awk script which remembers the fields from the previous line, and adds as many to the front as are missing to form a full line. Assume the first line indicates how many fields a full line should have. Read up on array handling in the awk documentation.
I am new to this awk command ...........can you give a sample code on this requirment....that would be a helpful.....

Thanks your response
charan
  #4 (permalink)  
Old 03-31-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393

Code:
>cat file
E100,0,5/29/1993,0,E001,E000,A,500000,41666.667,240.384615
0,1/23/1994,0,E001,E003,A,125400,10450,60.288462
0,6/4/1994,0,E001,E003,A,95000,7916.667,45.673077
E101,0,7/30/1993,0,E001,E003,A,87000,7250,41.826923
1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E103,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E104,0,2/15/1995,0,E001,E001,A,78000,6500,37.5


Code:
>awk 'NF!=10{for(i=1;i<=(10-NF);i++)printf("%s,",k[i]);print;next}{for(i=1;i<=3;i++)k[i]=$i}1' FS=',' f1
E100,0,5/29/1993,0,E001,E000,A,500000,41666.667,240.384615
E100,0,1/23/1994,0,E001,E003,A,125400,10450,60.288462
E100,0,6/4/1994,0,E001,E003,A,95000,7916.667,45.673077
E101,0,7/30/1993,0,E001,E003,A,87000,7250,41.826923
E101,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E103,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E104,0,2/15/1995,0,E001,E001,A,78000,6500,37.5

  #5 (permalink)  
Old 03-31-2008
ashish4422 ashish4422 is offline
Registered User
  
 

Join Date: Feb 2008
Location: india
Posts: 7
Hi Klashxx,

can you explain the logic used in for loop?

awk 'NF!=10{for(i=1;i<=(10-NF);i++)printf("%s,",k[i]);print;next}{for(i=1;i<=3;i++)k[i]=$i}1' FS=',' f1


Thanks
Ashish
  #6 (permalink)  
Old 03-31-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
It does pretty much what I suggested, except it hard-codes the number of expected fields to 10. You could count the number of fields on the first line and use that on subsequent lines for a slightly more general approach. It remembers $1 in k[1], $2 in k[2], and $3 in k[3]; and if the line has too few fields, prints what's in k[1], k[2], etc from the previous line as many as is required. This explanation is backwards relative to the script because it prints first, and then remembers for the next line.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0