Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help: Difference in awk execution.

Status
Not open for further replies.

prsdrane

Programmer
Jul 11, 2015
2
0
0
IN
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,
 
Hi

man awk said:
[tt]A missing action is equivalent to

[tab][tab]{ print }[/tt]

Code:
[navy]NR[/navy][teal]==[/teal][purple]1[/purple] [teal]{[/teal] [b]print[/b] [navy]$1[/navy] [teal]}[/teal]   [gray]# if record number is 1, print field 1[/gray]

[gray]# vs.[/gray]

[navy]NR[/navy][teal]==[/teal][purple]1[/purple]                [gray]# if record number is 1, perform default action[/gray]
[teal]{[/teal] [b]print[/b] [navy]$1[/navy] [teal]}[/teal]         [gray]# always print field 1[/gray]

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top