LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2005, 01:42 AM   #1
suchi_s
Member
 
Registered: May 2004
Posts: 133

Rep: Reputation: 15
awk code on linux


{
getline < "intervals"
while (getline != NULL)
{
getline < "intervals"
printf("%s\n" ,$0)
}
}

this is the code i have written
it gives its output perfectly
but for every record to see on screen i need to press enter why ?
kindly help
 
Old 01-13-2005, 02:04 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Try using this:

while ( getline < "intervals" > 0 )

Works for me, no human interaction needed while the file is processed.

Hope this helps.
 
Old 01-13-2005, 03:31 AM   #3
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
still it needs to press enter twice in the begining.. and at the end of the prog control D.
kindly help
 
Old 01-13-2005, 05:12 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The code snippet you gave does not show what it is you are trying to do. It looks like the hard way to do something (if intervals is the only file that is used).

If it is the only file, you do not need the while getline contruct. If there is another file, using the getline twice is too expensive (has to open, read, close the file twice).

If intervals is the only input file, try something like this:

awk '
{ print $0}
' intervals

The above snippet isn't too usefull, it just prints all the lines and fields. It's just a very simple example.

If more then one file is needed, some thing like this example is a way to handle this:

Code:
#!/usr/bin/ksh

awk 'BEGIN { FS=":" ; OFS="\t" ; print "User\tGID\tUser Name\tHome Dir\t"
              while ( getline < "/etc/group" > 0 )
              { GRID[$3] = $1 }
            }

{ print $1 , GRID[$4] , $5 , $6 }

END { print "\n End of Report \n" }

' /etc/passwd
The input files are /etc/passwd and /etc/groups, the human readable name of the group is printed instead of the GID number.

The BEGIN part reads the content of /etc/group and uses fields 3 and 1 to set up an array. The array is used to print the group name in the 'normal' (middle) part of awk.

I can explain why you have to press ctrl-d at the end: You read a line at the time (2nd getline), but do not check for an EOF. You pressing ctrl-d (=EOF) tells getline that it is done.

Hope this clears things up a bit.

Last edited by druuna; 01-13-2005 at 05:14 AM.
 
Old 01-13-2005, 08:45 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
good question.
Why are you using getline?

awk automatically reads files in.
you don't need to explicitly read a file.
 
Old 01-13-2005, 10:30 PM   #6
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
becoz i have to prerform calaulation on every field
 
Old 01-13-2005, 11:02 PM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
So parse it with for(i-0;i<NF;i++) or split() after setting the delimiter. Then calculate.

(NF=reserved variable, split = function)

HTH
END
 
  


Reply



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
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Clarification needed in this reg exp/awk code mselvam Programming 3 07-09-2005 05:26 PM
need clarification in this reg exp/awk code mselvam Linux - General 1 07-08-2005 03:57 PM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM
same awk script running on linux 7.2 and linux 10 suchi_s Programming 2 11-22-2004 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 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
Open Source Consulting | Domain Registration