Another question on awk - Begin statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Another question on awk - Begin statement
# 1  
Old 03-09-2005
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  
Old 03-09-2005
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  
Old 03-10-2005
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  
Old 03-10-2005
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  
Old 03-10-2005
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  
Old 03-10-2005
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Usage of a variable in awk BEGIN

Hi, diffcount=`awk 'BEGIN { while ( getline < "/scripts/matt/text.server1.reference" ) { arr++ } } { if (!( $0 in arr ) ) { print } }' $TMPDIR/$(basename $0 .sh) | wc -l` if ]; then OK="OK - No change in the interfaces status" elif ]; then DIFF=`awk 'BEGIN {... (4 Replies)
Discussion started by: nms
4 Replies

2. UNIX for Beginners Questions & Answers

Awk: use variable defined in begin

Hi there, I'm working with file more than 400K lines, 60 columns. Column count is going to be multiple of 12: 60, 12, 72 or so. NF/12 gives me on how many iterations I've to do to check certain value. For example: 7, 14th if only 24 columns in file. 7th, 14th and 21st if 36 columns in... (6 Replies)
Discussion started by: genome
6 Replies

3. Shell Programming and Scripting

Awk: BEGIN: prints nothing

My code fails to do anything if I've BEGIN block in it: Run the awk script as: awk -f ~/bin/sum_dupli_gene.awk make_gene_probe.txt #!/usr/bin/awk -f BEGIN { print ARGV #--loads of stuff } END{ #more stuff } (14 Replies)
Discussion started by: genome
14 Replies

4. UNIX for Dummies Questions & Answers

awk search with begin

Hi, I have written below script to begin if the line has n #!/bin/ksh /usr/xpg4/bin/awk {/ n / 'BEGIN {X = "01"; X = "02"; X = "03"; X = "04"; X = "05"; X = "06"; X = "07"; X = "08"; X ="09"; X = "10"; X = "11"; X = "12"; };} NR > 1 {print $1 "\t" $5 "," X "," $6 " " $7}'} input.txt |... (9 Replies)
Discussion started by: stew
9 Replies

5. Shell Programming and Scripting

BEGIN and END format in awk

I'm new to awk, trying to understand the basics. I'm trying to reset the counter everytime the program gets a new file to check. I figured in the BEGIN part it would work, but it doesn't. #!/bin/awk -f BEGIN {counter=0} { sum=0 for ( i=1; i<=NF;... (1 Reply)
Discussion started by: guitarist684
1 Replies

6. Shell Programming and Scripting

Perl: Question about 'BEGIN'

Newbie question, not sure of the use of BEGIN when you can just have the enclosed code inserted before the remaining program which means that code will get executed first anyway? (2 Replies)
Discussion started by: stevensw
2 Replies

7. Shell Programming and Scripting

awk BEGIN problem

awk 'BEGIN { print "line one\nline two\nline three" }' After ./awktest.sh Usage: awk -f progfile file ... Usage: awk 'program' file ... POSIX options: GNU long options: -f progfile --file=progfile -F fs --field-separator=fs -v var=val ... (7 Replies)
Discussion started by: cola
7 Replies

8. Shell Programming and Scripting

awk getting stuck after BEGIN

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (1 Reply)
Discussion started by: akshaykr2
1 Replies

9. Shell Programming and Scripting

Alias to awk BEGIN statement

I'd like to define an alias to awk's begin statement since I use awk with different delimiters all the time and it is tiresome to type awk '{OFS="\t";FS="\t"}{BLAH BLAH}' every time. The problem is that bash won't let me make an alias with an open quote, which is necessary for the BEGIN alias to... (3 Replies)
Discussion started by: baconbasher
3 Replies

10. Shell Programming and Scripting

AWK not processing BEGIN directive

Hello I have the following awk script: BEGIN { {FS = " " } {print "\t\tIllegal Loggon Attempts on MAIL\n"} {"date" | getline d} {printf "\t %s\n",d } {print "Loggon Name\t\t\t Number of Attempts\n"} ... (2 Replies)
Discussion started by: mojoman
2 Replies
Login or Register to Ask a Question