When I write the awk script it print one line multiple times
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have written this script it print same line multiple times. what to do if I want to print it only one time?
BEGIN print "Average of salary"
cnt=cnt+1
total=total+$3
avg=total/cnt
END printf "Number of records:%d avg is:%d",cnt,avg
awk
add a comment |Â
up vote
1
down vote
favorite
I have written this script it print same line multiple times. what to do if I want to print it only one time?
BEGIN print "Average of salary"
cnt=cnt+1
total=total+$3
avg=total/cnt
END printf "Number of records:%d avg is:%d",cnt,avg
awk
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have written this script it print same line multiple times. what to do if I want to print it only one time?
BEGIN print "Average of salary"
cnt=cnt+1
total=total+$3
avg=total/cnt
END printf "Number of records:%d avg is:%d",cnt,avg
awk
I have written this script it print same line multiple times. what to do if I want to print it only one time?
BEGIN print "Average of salary"
cnt=cnt+1
total=total+$3
avg=total/cnt
END printf "Number of records:%d avg is:%d",cnt,avg
awk
edited Apr 29 at 7:56
ñÃÂsýù÷
14.8k82462
14.8k82462
asked Apr 29 at 7:34
Shah Honey
173
173
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
The problem is that you didn't enclose the below code within braces as following and awk
will meet those as True statement and print every line it read:
cnt=cnt+1
total=total+$3
avg=total/cnt
But instead you could do something like:
awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile
The NR
value is incrementing on each record/line awk
reads from the input file, means that when awk read all lines the value of NR
is the line number of the last line (basically it points to the Record Number)
add a comment |Â
up vote
1
down vote
You almost make it.
enclose compute line in like
BEGIN ...
compute goes here
END printf "..."
why triple print ?
every assignation like foo=$3+foo
is a positive test to awk, and default action is to print line.
(and by the way, you only need to compute average in END
clause)
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The problem is that you didn't enclose the below code within braces as following and awk
will meet those as True statement and print every line it read:
cnt=cnt+1
total=total+$3
avg=total/cnt
But instead you could do something like:
awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile
The NR
value is incrementing on each record/line awk
reads from the input file, means that when awk read all lines the value of NR
is the line number of the last line (basically it points to the Record Number)
add a comment |Â
up vote
2
down vote
The problem is that you didn't enclose the below code within braces as following and awk
will meet those as True statement and print every line it read:
cnt=cnt+1
total=total+$3
avg=total/cnt
But instead you could do something like:
awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile
The NR
value is incrementing on each record/line awk
reads from the input file, means that when awk read all lines the value of NR
is the line number of the last line (basically it points to the Record Number)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The problem is that you didn't enclose the below code within braces as following and awk
will meet those as True statement and print every line it read:
cnt=cnt+1
total=total+$3
avg=total/cnt
But instead you could do something like:
awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile
The NR
value is incrementing on each record/line awk
reads from the input file, means that when awk read all lines the value of NR
is the line number of the last line (basically it points to the Record Number)
The problem is that you didn't enclose the below code within braces as following and awk
will meet those as True statement and print every line it read:
cnt=cnt+1
total=total+$3
avg=total/cnt
But instead you could do something like:
awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile
The NR
value is incrementing on each record/line awk
reads from the input file, means that when awk read all lines the value of NR
is the line number of the last line (basically it points to the Record Number)
edited Apr 29 at 8:07
answered Apr 29 at 7:53
ñÃÂsýù÷
14.8k82462
14.8k82462
add a comment |Â
add a comment |Â
up vote
1
down vote
You almost make it.
enclose compute line in like
BEGIN ...
compute goes here
END printf "..."
why triple print ?
every assignation like foo=$3+foo
is a positive test to awk, and default action is to print line.
(and by the way, you only need to compute average in END
clause)
add a comment |Â
up vote
1
down vote
You almost make it.
enclose compute line in like
BEGIN ...
compute goes here
END printf "..."
why triple print ?
every assignation like foo=$3+foo
is a positive test to awk, and default action is to print line.
(and by the way, you only need to compute average in END
clause)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You almost make it.
enclose compute line in like
BEGIN ...
compute goes here
END printf "..."
why triple print ?
every assignation like foo=$3+foo
is a positive test to awk, and default action is to print line.
(and by the way, you only need to compute average in END
clause)
You almost make it.
enclose compute line in like
BEGIN ...
compute goes here
END printf "..."
why triple print ?
every assignation like foo=$3+foo
is a positive test to awk, and default action is to print line.
(and by the way, you only need to compute average in END
clause)
answered Apr 29 at 7:53
Archemar
18.9k93365
18.9k93365
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440690%2fwhen-i-write-the-awk-script-it-print-one-line-multiple-times%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password