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
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 01:14 PM
IF statement question hcclnoodles Shell Programming and Scripting 6 02-02-2007 01:14 PM
I would like to begin programming in unix and linux jeet_ajay Linux 2 12-06-2006 09:25 AM
BEGIN failed--compilation aborted abhijeetkul UNIX for Advanced & Expert Users 1 03-21-2006 03:36 AM
Suggestions on where to begin? andrew25008 UNIX for Dummies Questions & Answers 4 12-28-2001 05:05 PM

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 03-09-2005
eff_cee eff_cee is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 3
Another question on awk - Begin statement

Hi,

I've a question on awk. In English I want to:
(a) open a file, (b) search through the file for records where length of field15 > 20 characters and (c) print out some fields in the record.

I've written the following and it works OK. The trouble is this will ALWAYS write out the column headings irrespective of whether any records match the > 20 characters rule. Thus an output file will always be created. I only want to output anything IF the search criteria is satisfied.

I've tried moving the BEGIN part to after the search predicate (ie the length$15 > 20), but awk doesn't seem to like it. From reading the documentation the BEGIN command is invoked before any file is opened for searching. Does this mean that the BEGIN must be at the start of the statement ? I could nest the code inside another if (awk?) statement, but there must be a cleverer way? Any ideas ?

awk -F, 'BEGIN {print '\""${ROWDELIM}\n\""' \
'\""${HEAD1}\n\""' \
'\""${ROWDELIM}\n\""' \
'\""${HEAD2}\""'} \
length($15) > 20 \
{print $1","$3","$15, ++n \
} END {print "RECORD COUNT IS: " n}' $INPUTFILE > $TEXTFILE


Thanks
  #2 (permalink)  
Old 03-09-2005
larryase larryase is offline
Registered User
  
 

Join Date: Nov 2004
Posts: 69
I think that you may want to put an IF statment in there.

awk -F, 'BEGIN {if( length($15) > 20 )print '\""${ROWDELIM}\n\""' \
'\""${HEAD1}\n\""' \
'\""${ROWDELIM}\n\""' \
'\""${HEAD2}\""'} \
{print $1","$3","$15, ++n \
} END {print "RECORD COUNT IS: " n}' $INPUTFILE > $TEXTFILE

I have not tested this but I know that you can use if statement. Let me know if this works.

Good Luck
  #3 (permalink)  
Old 03-10-2005
eff_cee eff_cee is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 3
Larry, Thanks for the prompt reply !

Interesting. Getting closer. I had to put back the line you moved (length($15) > 30 as your version would result in all records in $INPUTFILE getting output . I've simplified the code too . So now I have :

awk -F, 'BEGIN {if( length($15) > 30 ) print "HEADING RECORD"} \
length($15) > 30 \
{print $1","$3","$15, ++n \
} END {print "RECORD COUNT IS: " n}' $INPUTFILE > $TEXTFILE

BUT the if condition you moved to straight after the begin is not getting tested properly. ie the title "HEADING RECORD" is not being output (but the main body of the output is correct) Any ideas ?
  #4 (permalink)  
Old 03-10-2005
muthukumar muthukumar is offline
Registered User
  
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Quote:
Originally Posted by eff_cee
Hi,

I've a question on awk. In English I want to:
(a) open a file, (b) search through the file for records where length of field15 > 20 characters and (c) print out some fields in the record.

I've written the following and it works OK. The trouble is this will ALWAYS write out the column headings irrespective of whether any records match the > 20 characters rule. Thus an output file will always be created. I only want to output anything IF the search criteria is satisfied.

I've tried moving the BEGIN part to after the search predicate (ie the length$15 > 20), but awk doesn't seem to like it. From reading the documentation the BEGIN command is invoked before any file is opened for searching. Does this mean that the BEGIN must be at the start of the statement ? I could nest the code inside another if (awk?) statement, but there must be a cleverer way? Any ideas ?

awk -F, 'BEGIN {print '\""${ROWDELIM}\n\""' \
'\""${HEAD1}\n\""' \
'\""${ROWDELIM}\n\""' \
'\""${HEAD2}\""'} \
length($15) > 20 \
{print $1","$3","$15, ++n \
} END {print "RECORD COUNT IS: " n}' $INPUTFILE > $TEXTFILE


Thanks

BEGIN statement is used to initialize that text formatting operation. In your case, operation like field15 > 20 has to be done in the operational area.

what is there in ${ROWDELIM}, ... ${HEAD2} variable. You have to include with awk -v <variable>=value

plz. give your sample three line input and expected output so that it will be more easy to accomplish your requirement.

hth.
  #5 (permalink)  
Old 03-10-2005
eff_cee eff_cee is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 3
Ok, I've simplified and summarised my problem below (see first post for full description):

#Inputfile
1,Thisfieldis27characterslong,field3,field4
2,Thisfieldiss28characterslong,field3,field4
3,Thisfieldisss29characterslong,field3,field4

#Script
awk -F, 'BEGIN {if( length($2) > 28 ) print "HEADINGRECORD"} \
length($2) > 28 \
{print $1","$2, ++n} \
END {print "RECORD COUNT IS: " n}' inputfile > outputfile

#Expected outputfile:
HEADINGRECORD
3,Thisfieldisss29characterslong
RECORD COUNT IS: 1

Current outputfile:
3,Thisfieldisss29characterslong
RECORD COUNT IS: 1

If script rerun with the length increased to 29 (ie length($2) > 29), then there should be NO output (so, whatever solution there is for the BEGIN output, I'll need to apply to the END too)
  #6 (permalink)  
Old 03-10-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Use a flag...
Code:
awk -F, -v header="blah" '
   length($15) > 20 {
       if (flg==0) {
           print header
           flg = 1
        }
        print $1","$3","$15, ++n
    } 
    END {
        print "RECORD COUNT IS: " n
    }' $INPUTFILE > $TEXTFILE
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 02:49 PM.


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