INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Please help: Difference in awk execution.

Please help: Difference in awk execution.

(OP)
input file name:checkbook.txt (fields are tab separated)
1000
125 Market 125.45
126 Hardware Store 34.95
127 Video Store 7.45
128 Book Store 14.32
129 Gasoline 16.10

I need first record first field only

For the above file I get differnce in output as below:
1. I tried below command and it worked:

[oracle@localhost scripts]$ awk 'NR==1 { print $1 }' /home/oracle/Documents/myprojects/Documents/dir2/checkbook.txt
1000

2. The same command in a file Checkbook.awk gives output:
[oracle@localhost scripts]$ awk -f Checkbook.awk /home/oracle/Documents/myprojects/Documents/dir2/checkbook.txt
1000
1000
125
126
127
128
129


End

Code in Checkbook.awk

[oracle@localhost scripts]$ cat Checkbook.awk
BEGIN {
}
NR==1
{
print $1
}

END {
print "\n\nEnd"
}

Also tried altering the Checkbook.awk as below,

[oracle@localhost scripts]$ cat Checkbook.awk
NR==1
{
print $1
}[oracle@localhost scripts]$ awk -f Checkbook.awk /home/oracle/Documents/myprojects/Documents/dir2/checkbook.txt
1000
1000
125
126
127
128
129
[oracle@localhost scripts]$


Why is difference in output?
Please help to resolve it.

Regards,

RE: Please help: Difference in awk execution.

Hi

Quote (man awk)

A missing action is equivalent to

        { print }

CODE --> Awk

NR==1 { print $1 }   # if record number is 1, print field 1

# vs.

NR==1                # if record number is 1, perform default action
{ print $1 }         # always print field 1 

Feherke.
feherke.ga

RE: Please help: Difference in awk execution.

(OP)
Thank you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close