awk BEGIN END and string matching problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk BEGIN END and string matching problem
# 1  
Old 08-18-2009
awk BEGIN END and string matching problem

Hi,
Contents of BBS-list file:
Code:
foo
foo
foo

Code:
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }
END   { print "\"foo\" appears", n, "times." }' BBS-list

Output:
Code:
Analysis of "foo"
"foo" appears 3 times.

Code:
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }
++n
END   { print "\"foo\" appears", n, "times." }' BBS-list

Output:
Code:
Analysis of "foo"
foo 
foo
foo
"foo" appears 6 times.

Why does it print the contents of BBS-list file if i add ++n?
# 2  
Old 08-18-2009
Quote:
Originally Posted by cola
Hi,
Contents of BBS-list file:
Code:
foo
foo
foo

Code:
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }
END   { print "\"foo\" appears", n, "times." }' BBS-list

Output:
Code:
Analysis of "foo"
"foo" appears 3 times.

Code:
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }
++n
END   { print "\"foo\" appears", n, "times." }' BBS-list

Output:
Code:
Analysis of "foo"
foo 
foo
foo
"foo" appears 6 times.

Why does it print the contents of BBS-list file if i add ++n?
'++n' standing 'alone' is a condition with a missing 'action' - no corresponding '{...}'.
As '++n' (condition) is incremented and is NOT '0'. The default 'action' for a non-zero 'condition' is to print an entire current record/line.
# 3  
Old 08-18-2009
Try this as example and see the output...
1 means true, in ur example, n++ is always positive so it prints for u all records...
Code:
awk '
BEGIN { print "Analysis of \"foo\"" }
/foo/ { ++n }1' BBS-list

# 4  
Old 08-18-2009
Quote:
Originally Posted by vgersh99
'++n' standing 'alone' is a condition with a missing 'action' - no corresponding '{...}'.
As '++n' (condition) is incremented and is NOT '0'. The default 'action' for a non-zero 'condition' is to print an entire current record/line.
Thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do delete certain lines alone which are matching with start and end string values in file?

Hi, In my previous post ( How to print lines from a files with specific start and end patterns and pick only the last lines? ), i have got a help to get the last select statement from a file, now i need to remove/exclude the output from main file: Input File format: SELECT ABCD, DEFGH,... (2 Replies)
Discussion started by: nani2019
2 Replies

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

3. Shell Programming and Scripting

Begin/End blocks in awk: confused

I am trying to understand how to use the END block in awk without much success. I have this script that I found: gawk '{count++; keyword = $1} if (count == 3) keyword = "order this" else print keyword " " k } }' << orderfile Is that the way that the END block should be used? I am... (6 Replies)
Discussion started by: newbie2010
6 Replies

4. Shell Programming and Scripting

strange: sed and awk print at end instead of begin of line

Hi! I have a strange behaviour from sed and awk, but I'm not sure, if I'm doing something wrong: I have a list of words, where I want to add the following string at the end of each line: \;\;\;\;0\;1 I try like this: $ cat myfile | awk '{if ( $0 != "" ) print $0"\;\;\;\;0\;1"}' Result:... (5 Replies)
Discussion started by: regisl67
5 Replies

5. Shell Programming and Scripting

Use of Begin IF ,END IF END not working in the sql script

Hi I have written a script .The script runs properly if i write sql queries .But if i use PLSQL commands of BEGIN if end if , end ,then on running the script the comamds are getting printed on the prompt . Ex :temp.sql After connecting to the databse at the sql prompt i type... (1 Reply)
Discussion started by: isha_1
1 Replies

6. Shell Programming and Scripting

How to Print from matching word to end using awk

Team, Could some one help me in Printing from matching word to end using awk For ex: Input: I am tester for now I am tester yesterday I am tester tomorrow O/p tester for now tester yesterday tester tomorrow i.e Starting from tester till end of sentence (5 Replies)
Discussion started by: mallak
5 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

begin end detection

Hi all, i am new to scripting. i need to write a code to detect begin and end of word that either begins or ends with t,th,d,dh,s,sh i have a set of words in a file containg one word per line. let the filename be aaa.txt. i have an another file bbb.txt which has two lines, just specifying the... (7 Replies)
Discussion started by: blkanth
7 Replies

9. Shell Programming and Scripting

BEGIN END questions

Why would you need to use this in a script? Why can't you just use print to print out what you want printed in the begining and print for what you want at the end. So this: nawk 'BEGIN {print "this is the first line"} {print $1 $2 $3} {print $5 $6} END {print "this is the last line"}' ... (2 Replies)
Discussion started by: llsmr777
2 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question