LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2010, 04:47 PM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
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.
 
Old 11-14-2010, 04:56 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Hi,

try this:
Code:
awk '{a[FNR]=$1}END{FNR--;for (i=1;i<=FNR;i++){print a[i]/FNR}}' file
 
Old 11-14-2010, 04:59 PM   #3
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
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.
 
Old 11-15-2010, 07:41 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 07:23 AM
Awk question . maas187 Linux - Software 1 09-10-2009 07:19 PM
awk question puishor Programming 2 08-26-2005 10:44 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 02:37 PM
awk question jonlake Linux - Software 2 05-04-2005 04:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration