![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
| Rename file based on first 3 characters of data in file | jchappel | UNIX for Dummies Questions & Answers | 3 | 10-01-2008 02:21 PM |
| Split File Based on Line Number Pattern | shankster | Shell Programming and Scripting | 11 | 10-01-2008 09:49 AM |
| how to change a particular value in a file based on a pattern | orbeyen | UNIX for Dummies Questions & Answers | 9 | 08-20-2008 03:09 AM |
| Split a file based on pattern in awk, grep, sed or perl | kumarn | Shell Programming and Scripting | 5 | 06-20-2008 10:51 AM |
| Extracting data from text file based on configuration set in config file | suparnbector | Shell Programming and Scripting | 3 | 08-10-2007 02:25 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Truncating FILE data BASED ON A PATTERN
HI I HAVE A PROBLEM,MY SOURCE FILE IS OF PATTERN
S1,E-Certified,29,29,2.7,Certified,4,3,2.7,,0,0,0 S2,Certified,4,3,2.7,,0,0,0,,0 S3,E-Certified,29,29,2.7,,0,0,0 S4,,0,0,0,,0,0,0,,0,0,0,,0,0,0 AND THE EXPECTED OUTPUT IS S1,E-Certified,29,29,2.7 S1,Certified,4,3,2.7 S2,Certified,4,3,2.7 S3,E-Certified,29,29,2.7 THE NUMBER OF FIELDS(NF) IN SOURCE FILE MAY BE MORE THAN 500. WHEREEVER THE FIRST NULL VALUE COMES IT SHOULD TRUNCATE THE ROW. I TRIED TO SOLVE N WROTE THIS CODE, Code:
awk -F\, '{
if ($2!="") printf$1FS fi;c=0
for(i=2;i<=NF;i++)
{
if ($i=="") i=NF+1 fi;
printf$i ($i!=""?++c%4?i==NF?RS:FS:(i<NF?RS$1FS:RS):EXIT);
}
}'
S1,E-Certified,29,29,2.7 S1,Certified,4,3,2.7 S1,S2,Certified,4,3,2.7 S2,S3,E-Certified,29,29,2.7 S3, I AM NOT ABLE TO FIND OUT THE ERROR. IF ANYONE FINDS ANY PROBABLE SOLUTION PLZ SUGGEST ME. I Solved the problem. The code should be awk -F\, '{ if ($2!="") printf$1FS fi;c=0 for(i=2;i<=NF;i++) { if ($i=="") i+=NF fi; printf$i ($i!=""?++c%4?i==NF?RS:FS $(i+1)!=""?RS$1FS:RS):EXIT);} }' Last edited by pkumar3; 10-15-2008 at 12:49 AM.. Reason: added code tags |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|