The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

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

Join Date: Oct 2008
Posts: 7
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);
}
        }'
bUT THE OUTPUT COMING IS
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
  #2 (permalink)  
Old 10-14-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
What's going on when field 2 is blank?
If I understand you correctly, you want:
Code:
awk  'BEGIN { FS=OFS="," } !length($2) { next; } {
   for (i=3;i<=NF;++i) {
      if ($i=="") { NF=i-1; break;}
   }
   print $0;
}'
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:44 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