1

I have a text file named prac which has the numbers 1,2,3,4,5 in seperate lines. I want to generate the squares of these numbers using AWK command.

For this i wrote the following command:

awk 'BEGIN { a = $1; a = a ^ 2; print "a =", a }' prac

But the output shows a = 0

Please let me know my mistakes

1 Answer 1

3

Yes, because your code is in a BEGIN block. The special BEGIN block is triggered once, just before reading the the first line of input from the first input file (likewise, the special END block is triggered once after reading the last line of input). You want to execute your block of code for each line:

awk '{ printf("a = %s\n", $1*$1) }' prac

or closer to your original code,

awk '{ a = $1*$1; print "a =", a }' prac
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.