LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   another awk question (https://www.linuxquestions.org/questions/linux-newbie-8/another-awk-question-844235/)

ghantauke 11-14-2010 03:47 PM

another awk question
 
I have a file called test.txt which has the following data.

35554 the
20343 and
17130 to
15331 of
12130 a

I want to divide each element in the first column with the number of total lines in the file i.e 5 in this case. I tried using the following command to do it.
awk '{print $1/NR}' test.txt

It doesn't work. Could anyone please help.

crts 11-14-2010 03:56 PM

Hi,

try this:
Code:

awk '{a[FNR]=$1}END{FNR--;for (i=1;i<=FNR;i++){print a[i]/FNR}}' file

ghantauke 11-14-2010 03:59 PM

nevermind. i figured out a solution :)

x=wc -l <test
awk '{print $1/'$x'}' test

i kept using $x instead of '$x' before so i had to resort to using NR to no avail.

grail 11-15-2010 06:41 AM

Assuming you still want the rest of the line you could also do:
Code:

awk '{first[NR]=$1;rest[NR]=gensub($1,"",1)}END{for(i=1;i<=NR;i++)print first[i]/NR rest[i]}' test.txt


All times are GMT -5. The time now is 09:15 AM.