![]() |
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 |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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 ? |
|
||||
|
Quote:
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. |
|
||||
|
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) |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|